In Java, what is the Boolean "order of operations"?

Let's take a simple example of a Cat object. I want to be sure that the non-null Cat either orange or gray.

 if(cat != null && cat.getColor() == "orange" || cat.getColor() == "grey") { //do stuff } 

I believe, And in the first place, then in OR. I'm a little fuzzy, so here are my questions:

  • Can someone get me through this statement, so I'm sure I'm getting what is happening?

  • Also, what happens if I add brackets; Does the order of operations change?

  • Will my order of operations change from language to language?

+43
java operator-precedence logic boolean evaluation
Feb 15 '10 at 2:00
source share
6 answers

Java tutorials have a list illustrating operator priority . First, the equality operators will be executed, then && , then || . The brackets will be evaluated first, so adding them can change the order. This is usually almost the same from language to language, but it is always recommended to double check.

Small variations in behavior that you do not expect can result in a whole day of debugging, so itโ€™s recommended that you place parentheses so that you are sure what the evaluation order will be.

+51
Feb 15 '10 at 2:08
source share

The logical order of operations (in all languages โ€‹โ€‹that I consider):

  • round brackets
  • NOT
  • AND
  • OR

So your logic above is equivalent:

 (cat != null && cat.getColor() == "orange") || cat.getColor() == "grey" 
+13
Feb 15 '10 at 2:05
source share

The expression is basically identical to the expression:

 if ( (cat != null && cat.getColor() == "orange") || cat.getColor() == "grey") { ... } 

The priority order here is that AND ( && ) has a higher priority than OR ( || ).

You should also know that using == to check for equality of strings will sometimes work in Java, but thatโ€™s not how you should do it. You must do:

 if (cat != null && ("orange".equals(cat.getColor()) || "grey".equals(cat.getColor()))) { ... } 

those. use equals() methods to compare String , not == , which simply refer to equality. Link string equality can be misleading. For example:

 String a = new String("hello"); String b = new String("hello"); System.out.println(a == b); // false 
+8
Feb 15 2018-10-15T00
source share

First, the if statement contains three main expressions:

  • cat! = null
  • cat.getColor () == "orange"
  • cat.getColor () == "gray"

The first expression simply checks if cat is not empty. Its necessary otherwise, the second expression will be executed and will lead to NPE(null pointer excpetion) . Therefore, the use of && between the first and second expression. When you use && , if the first expression evaluates to false, the second expression never executes. Finally, you check to see if the cat's color is gray.

Finally, note that your if statement is still incorrect , because if cat is null, the third expression is still executed, and therefore you get a null pointer exception .

The correct way to do this is:

 if(cat != null && (cat.getColor() == "orange" || cat.getColor() == "grey")) { //do stuff } 

Check the order of the brackets.

+4
Feb 15 '10 at 2:05
source share

Yes && definitely rated to || . But I see that you are doing cat.getColor() == "orange" , which may give you an unexpected result. Instead, you might want to:

 if(cat != null && ("orange".equals(cat.getColor()) || "grey".equals(cat.getColor()))) { //do stuff } 
+3
Feb 15 '10 at 3:01
source share

The order of work is not what you need, you need logical algebra, it includes logical functions. Maxterms / minterms, gray code, Karnaugh tables, diodes, transistors, logic gates, multiplexers, beaters, triggers ... You want to implement logical "logic" on computers or virtual machines. With the "order of operations", can you refer to physics in the same way as controlling delays at logical outputs (OR, if) in nanosecond intervals?

-2
Jun 27 '15 at 4:49
source share



All Articles