不短路操作员

时间:2018-09-19 22:07:50

标签: java

我了解这部分:

    Integer myInt;
    myInt = new Integer(42);
    if (myInt != null && myInt.intValue() == 42) {
        System.out.println("Comparing 42 to 42");
    }
    myInt = null;
    if (myInt != null & myInt.intValue() == 42) {
        System.out.println("Comparing null to 42");
    }

输出:

Comparing 42 to 42
Exception in thread "main" java.lang.NullPointerException 

我不明白的是:

System.out.println("Result:" +(1 & 3));
System.out.println("Result2:" +(2 | 3));
Result:1
Result2:3

如果不比较布尔值,为什么要给出这个结果?

0 个答案:

没有答案