# Short Circuiting?

• 11-11-2009, 10:24 AM
Philly0494
Short Circuiting?
Alright, I've been trying to figure this out for a very long time, and I haven't been able to find an answer.

I know that in Java, the list of operator ranks are as found here:

java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html

which means logical operators are towards the bottom of that list.

if (2 == 2 || x++ * 2 == y-4)

Since most/all of the operations on the right side of the logical OR statement take precedence over the logical OR, will they be executed first? If so, wouldn't this make short circuiting useless since it is supposed to only evaluate the right side of the equation if the left side happens to return FALSE?

Thanks,
Philly0494
• 11-11-2009, 11:21 AM
PhHein
Actually the short circuiting still works:
Code:

```int x = 0;                 if(2 == 2 || ++x == 1){         System.out.println("x= " + x); } if(2 == 3 || ++x == 1){         System.out.println("x= " + x); } System.out.println("x= " + x);```
• 11-11-2009, 11:25 AM
Philly0494
so nothing on the right side of the logical OR will be executed? despite its operator rank, correct?
• 11-11-2009, 11:27 AM
PhHein
Yep, just play with that code and try some variations.
• 11-11-2009, 11:28 AM
Philly0494
hmmm

so is this a blatant violation of the operator rankings?

why does this occur, and does it occur anywhere else in java?
• 11-11-2009, 11:31 AM
JosAH
Quote:

Originally Posted by Philly0494
hmmm

so is this a blatant violation of the operator rankings?

why does this occur, and does it occur anywhere else in java?

It isn't a violation at all; for the expression (a || b) first the value of the left sub expression 'a' is evaluated; then the short circuiting sets in: if that value is true the right sub expression 'b' isn't evaluated because the value of the entire expression is true as well. If you don't like short circuiting use the other operator '|' instead.

kind regards,

Jos
• 11-11-2009, 11:36 AM
Philly0494
Quote:

Originally Posted by JosAH
It isn't a violation at all; for the expression (a || b) first the value of the left sub expression 'a' is evaluated; then the short circuiting sets in: if that value is true the right sub expression 'b' isn't evaluated because the value of the entire expression is true as well. If you don't like short circuiting use the other operator '|' instead.

kind regards,

Jos

but the "||" is a lower rank than "++" and many other operators, which means those operators should be executed first, before java even knows whether its an AND, OR, or neither

that is what I was taught but it is clearly not true, still trying to figure it out.
• 11-11-2009, 11:38 AM
PhHein
IMHO no. read this and the following chapters of the JLS: Expressions
• 11-11-2009, 11:46 AM
Tolls
For the example you've given the compiler will actually remove the check entirely (try it and look at the byte code). Replacing the "2==2" with "a==2" results in byte code that evaluates the left first and only if not true wll it evaluate the second part (as expected).

Short-circuiting is an exception, if you like, to the precedence list.

Edit: Oh and (reading the stuff that was written since I opened this page) it's also how any other language I've worked with has done it.
• 11-11-2009, 03:40 PM
JosAH
Be glad that short circuiting works the way it works; otherwise the following would fail miserably:

Code:

```PowerPlant harrisburg= new PowerPlant(); if (harrisburg.isOK() && harrisburg.switchOn())   System.out.println("Everything's fine here!");```
kind regards,

Jos
• 11-11-2009, 06:51 PM
Philly0494
Quote:

Originally Posted by JosAH
Be glad that short circuiting works the way it works; otherwise the following would fail miserably:

Code:

```PowerPlant harrisburg= new PowerPlant(); if (harrisburg.isOK() && harrisburg.switchOn())   System.out.println("Everything's fine here!");```
kind regards,

Jos

It's not like I'm against short circuiting, I am just trying to understand why it overrides the precedence list...

The only way I can grasps this is if the compiler actually redefines the if statement behind the scenes like this:

Code:

```if( CONDITION_A && CONDITION_B ) {   // TRUE_BRANCH } else {   // FALSE_BRANCH }```
will be rewritten to
Code:

```if( CONDITION_A ) {     if( CONDITION_B ) {         // TRUE_BRANCH     } else {         // FALSE_BRANCH (B_FALSE)     } } else {     // FALSE_BRANCH (A_FALSE) }```