我对此并不满意:
http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.22.它清楚地说明了以下内容:
“Each operator is commutative if the operand expressions have no side effects.”
和
“These operators have different precedence, with & having the highest precedence and | the lowest precedence”.
为什么JVM不符合它自己的规则.以下面的例子为例.
public static void main(String[] args){
boolean bool = isTrue1() | isFalse1() & isFalse2() ;
System.out.println("Result : " + bool);
}
public static boolean isFalse1() {
System.out.println("1 : " + false);
return false ;
}
public static boolean isFalse2() {
System.out.println("2 : " + false);
return false ;
}
public static boolean isTrue1() {
System.out.println("3 : " + true);
return true ;
}
结果是:
3 : true
1 : false
2 : false
Result : true
虽然实际结果应该是,根据&在|之前评估运算符操作符:
1 : false
2 : false
3 : true
Result : true
一些解释很好,为什么这没有正确实现.即使在第二部分周围添加括号,也不会使用正确的优先级.