Results 1 to 13 of 13
  1. #1
    Philly0494 is offline Member
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

    Default 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. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    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);
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Philly0494 is offline Member
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

    Default

    so nothing on the right side of the logical OR will be executed? despite its operator rank, correct?

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Yep, just play with that code and try some variations.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    Philly0494 is offline Member
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

    Default

    hmmm

    so is this a blatant violation of the operator rankings?

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

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Philly0494 View Post
    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. #7
    Philly0494 is offline Member
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    IMHO no. read this and the following chapters of the JLS: Expressions
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    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. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    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. #11
    Philly0494 is offline Member
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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)
    }
    Link:en.wikipedia.org/wiki/Short-circuit_evaluation#Untested_second_condition_leads _to_unperformed_side-effect

    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. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

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

    kind regards,

    Jos

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    As I suggested above, look at the byte code.
    It does essentially produce the flow you have.

Similar Threads

  1. Passing short value as parameter
    By javanewbie83 in forum New To Java
    Replies: 16
    Last Post: 07-16-2008, 06:27 AM
  2. Short-cut key
    By tskumarme in forum New To Java
    Replies: 2
    Last Post: 05-28-2008, 05:16 PM
  3. why doesn't this short applet work?
    By kim85 in forum New To Java
    Replies: 1
    Last Post: 01-20-2008, 09:43 PM
  4. Short/Integer
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-06-2007, 10:28 PM

Posting Permissions

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