class test {
public static void main (String[] args) {
boolean a = false;
boolean b = true;
if (a && a || b) {
System.out.println(true);
}
}
} //--why it always true????o/p is true but why??
最佳答案
操作顺序。
&&
比 ||
具有更高的优先级,因此被评估首先。您的 if
条件可以重写如下:
(a && a) || b
(false && false) || true
false || true
true
此条件将始终为 false || true
对于您列出的条件始终为 true
。
检查 here用于 Oracle 的官方表格,其中列出了所有运算符的优先级。
关于java条件运算符,为什么下面的代码输出为真?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34690391/