问题描述
让我们看一个物体猫
的一个简单的例子。我想肯定了不空猫
为橙色或灰色。
如果(猫= NULL&放大器;!&安培; cat.getColor()==橙色|| cat.getColor()==灰色){
//做东西
}
我相信并且是第一位的,然后是OR。我有点模糊的,所以这里是我的问题:
-
有人能走我通过这句话,所以我敢肯定,我得到了什么情况?
-
另外,如果我加括号会发生什么;这是否改变操作的顺序?
-
请问我的操作顺序,可改变从语言到语言?
Java教程有说明的。平等的运营商将首先计算,那么&放大器;&安培;
,那么 ||
。括号将任何事情之前进行评估,因此增加他们可以更改顺序。这通常是pretty多从语言到语言相同,但它总是一个好主意,仔细检查。
这是在行为上的微小变化,你不期望能引起你花了整整一天的调试,那么那么你肯定评价的顺序将是什么它是把括号的地方是个好主意。
Let's take a simple example of an object Cat
. I want to be sure the "not null" cat
is either orange or grey.
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:
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?
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.
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.
这篇关于在Java中,什么是布尔"操作顺序及QUOT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!