在 Java 中,布尔“操作顺序”是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2263660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Java, what are the boolean "order of operations"?
提问by Stephano
Let's take a simple example of an object Cat
. I want to be sure the "not null" cat
is either orange or grey.
让我们举一个对象的简单例子Cat
。我想确定“非空”cat
是橙色或灰色。
if(cat != null && cat.getColor() == "orange" || cat.getColor() == "grey") {
//do stuff
}
I believe AND comes first, then the OR. I'm kinda fuzzy though, so here are my questions:
我相信 AND 是第一位的,然后是 OR。我有点模糊,所以这里是我的问题:
Can someone walk me through this statement so I'm sure I get what happens?
Also, what happens if I add parentheses; does that change the order of operations?
Will my order of operations change from language to language?
有人可以引导我完成此声明,以便我确定我明白会发生什么吗?
另外,如果我添加括号会发生什么?这会改变操作顺序吗?
我的操作顺序会因语言而异吗?
采纳答案by Bill the Lizard
The Java Tutorials has a list illustrating operator precedence. The equality operators will be evaluated first, then &&
, then ||
. Parentheses will be evaluated before anything else, so adding them can change the order. This is usually pretty much the same from language to language, but it's always a good idea to double check.
Java 教程有一个说明运算符优先级的列表。将首先评估相等运算符,然后是&&
,然后是||
。括号将在其他任何事情之前进行评估,因此添加它们可以更改顺序。这通常从语言到语言几乎相同,但仔细检查总是一个好主意。
It's the small variations in behavior that you're not expecting that can cause you to spend an entire day debugging, so it's a good idea to put the parentheses in place so you're sure what the order of evaluation will be.
正是您没有预料到的行为上的微小变化会导致您花费一整天的时间进行调试,因此最好将括号放在适当的位置,这样您就可以确定评估的顺序是什么。
回答by Trey Hunner
Boolean order of operations (in all languages I believe):
布尔运算顺序(我相信在所有语言中):
- parens
- NOT
- AND
- OR
- 父母
- 不是
- 和
- 或者
So your logic above is equivalent to:
所以你上面的逻辑相当于:
(cat != null && cat.getColor() == "orange") || cat.getColor() == "grey"
回答by Suraj Chandran
First, your if statement contains three main expressions:
首先,您的 if 语句包含三个主要表达式:
- cat != null
- cat.getColor() == "orange"
- cat.getColor() == "grey"
- 猫 != 空
- cat.getColor() == "橙色"
- cat.getColor() == "灰色"
The first expression simply checks whether cat is not null. Its necessary otherwise the the second expression will get executed and will result in a NPE(null pointer excpetion)
. That's why the use of && between the first and second expression. When you use &&
, if the first expression evaluates to false the second expression is never executed.
Finally you check whether the cat's color is grey.
第一个表达式只是检查 cat 是否不为空。这是必要的,否则第二个表达式将被执行并导致NPE(null pointer excpetion)
. 这就是为什么在第一个和第二个表达式之间使用 && 的原因。使用时&&
,如果第一个表达式的计算结果为 false,则永远不会执行第二个表达式。最后检查猫的颜色是否为灰色。
Finally note that your if statement is still wrongbecause if cat is null, the third expression is still executed and hence you get a null pointer exception.
最后请注意,您的 if 语句仍然是错误的,因为如果 cat 为空,则仍会执行第三个表达式,因此您会收到空指针异常。
The right way of doing it is:
正确的做法是:
if(cat != null && (cat.getColor() == "orange" || cat.getColor() == "grey")) {
//do stuff
}
Check the order of parenthesis.
检查括号的顺序。
回答by cletus
The expression is basically identical to:
该表达式基本上等同于:
if ( (cat != null && cat.getColor() == "orange") || cat.getColor() == "grey") {
...
}
The order of precedence here is that AND (&&
) has higher precedence than OR (||
).
这里的优先顺序是 AND ( &&
) 的优先级高于 OR ( ||
)。
You should also know that using ==
to test for String equality will sometimeswork in Java but it is not how you should do it. You should do:
您还应该知道,有时在 Java中使用==
来测试 String 相等性会起作用,但这不是您应该这样做的方式。你应该做:
if (cat != null && ("orange".equals(cat.getColor()) || "grey".equals(cat.getColor()))) {
...
}
ie use the equals()
methods for String
comparison, not ==
which simply does reference equality. Reference equality for strings can be misleading. For example:
即使用equals()
方法进行String
比较,而不是==
简单地引用相等。字符串的引用相等性可能会产生误导。例如:
String a = new String("hello");
String b = new String("hello");
System.out.println(a == b); // false
回答by fastcodejava
Yeah &&
is definitely evaluated before ||
. But I see you are doing cat.getColor() == "orange"
which might give you unexpected result. You may want to this instead :
&&
肯定是评价过呀||
。但是我看到您正在做的事情cat.getColor() == "orange"
可能会给您带来意想不到的结果。你可能想要这个:
if(cat != null && ("orange".equals(cat.getColor()) || "grey".equals(cat.getColor()))) {
//do stuff
}
回答by Kirschbaum
Order of Operation is not what you need, you need boolean algebra, this includes boolean functions. Maxterms/minterms, Gray code, Karnaugh tables, diodes,transistors, logic gates, multiplexers, bitadders, flip flops... What you want is to implement boolean "logic" on computers or virtual machines. With "order of operations" you may refer something about physics like managing delays on logic gates (OR, if) nanoseconds intervals?
操作顺序不是你需要的,你需要布尔代数,这包括布尔函数。Maxterms/minterms、格雷码、卡诺表、二极管、晶体管、逻辑门、多路复用器、bitadders、触发器......你想要的是在计算机或虚拟机上实现布尔“逻辑”。通过“操作顺序”,您可能会参考一些有关物理的内容,例如管理逻辑门(OR,if)纳秒间隔的延迟?