1. Member
Join Date
Nov 2009
Posts
5
Rep Power
0

## 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

2. Actually the short circuiting still works:
Java 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);```

3. Member
Join Date
Nov 2009
Posts
5
Rep Power
0
so nothing on the right side of the logical OR will be executed? despite its operator rank, correct?

4. Yep, just play with that code and try some variations.

5. Member
Join Date
Nov 2009
Posts
5
Rep Power
0
hmmm

so is this a blatant violation of the operator rankings?

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

6. 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

7. Member
Join Date
Nov 2009
Posts
5
Rep Power
0
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.

8. IMHO no. read this and the following chapters of the JLS: Expressions

9. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18
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.

10. Be glad that short circuiting works the way it works; otherwise the following would fail miserably:

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

Jos

11. Member
Join Date
Nov 2009
Posts
5
Rep Power
0
Originally Posted by JosAH
Be glad that short circuiting works the way it works; otherwise the following would fail miserably:

Java 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:

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

so is this actually how its done? and this is the real reason why it overrides the precedence list? not because its an exception per se, but because it separates the statements themselves.

12. Yes, that is conceptually how it's done. Short circuiting is just another (quite powerful) mechanism next to operator precedence.

kind regards,

Jos

13. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18
As I suggested above, look at the byte code.
It does essentially produce the flow you have.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•