Operator precedence

Discussion in 'Java' started by Ryan Chan, May 8, 2010.

  1. Ryan Chan

    Ryan Chan Guest

    Hi, some simple question about the Operator precedence



    int i = 9;
    System.out.println(i++ + 2);

    Why it print 11 instead of 12, assume operator is evaluated from left
    to right?
    Ryan Chan, May 8, 2010
    #1
    1. Advertising

  2. Ryan Chan

    Lew Guest

    On 05/08/2010 09:48 AM, Ryan Chan wrote:
    > int i = 9;
    > System.out.println(i++ + 2);
    >
    > Why it print 11 instead of 12, assume operator is evaluated from left
    > to right?


    What is the definition of postincrement?

    To put another way, given:

    int i = 9;
    int j = i++;

    what is the value of j?

    It has nothing to do with precedence.

    --
    Lew
    Lew, May 8, 2010
    #2
    1. Advertising

  3. Ryan Chan

    markspace Guest

    Ryan Chan wrote:
    > Hi, some simple question about the Operator precedence
    >
    >
    >
    > int i = 9;
    > System.out.println(i++ + 2);
    >
    > Why it print 11 instead of 12, assume operator is evaluated from left
    > to right?
    >



    Yeah, it's POST increment. That means i is incremented AFTER it's value
    (9) is used. Not a precedence issue. PEBCAK issue.
    markspace, May 8, 2010
    #3
  4. Ryan Chan

    Lew Guest

    Ryan Chan wrote:
    >> int i = 9;
    >> System.out.println(i++ + 2);
    >>
    >> Why it print 11 instead of 12, assume operator is evaluated from left
    >> to right?


    markspace wrote:
    > Yeah, it's POST increment. That means i is incremented AFTER it's value
    > (9) is used. Not a precedence issue. PEBCAK issue.


    OP: Consider reading the documentation.

    The Java tutorial explains this sort of thing nicely:
    <http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op1.html>
    and, of course, the Java Language Specification is the authority on the matter:
    <http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#292383>

    And there's no need to assume the order of evaluation; it's specified by the
    language's rules.

    --
    Lew
    Lew, May 8, 2010
    #4
  5. Ryan Chan

    EJP Guest

    On 8/05/2010 11:48 PM, Ryan Chan wrote:
    > Why it print 11 instead of 12, assume operator is evaluated from left
    > to right?


    Apart from what the other respondents have said, operators are not
    evaluated left to right. Neither are operands other than those around a
    single binary operator.
    EJP, May 9, 2010
    #5
  6. Ryan Chan

    Lew Guest

    On 05/09/2010 12:51 AM, EJP wrote:
    > On 8/05/2010 11:48 PM, Ryan Chan wrote:
    >> Why it print 11 instead of 12, assume operator is evaluated from left
    >> to right?

    >
    > Apart from what the other respondents have said, operators are not
    > evaluated left to right. Neither are operands other than those around a
    > single binary operator.


    Here is the actual rule:
    <http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.7>
    "The Java programming language guarantees that the operands of operators
    appear to be evaluated in a specific evaluation order, namely, from left to
    right."

    This applies as well to operands of the ternary operator.

    One should read the rest of s. 15.7 to understand the effects of things like
    parentheses.

    --
    Lew
    Lew, May 9, 2010
    #6
  7. Ryan Chan

    EJP Guest

    On 9/05/2010 3:20 PM, Lew wrote:

    Thanks, so I should have said 'Neither are operands other than those
    around a single binary or ternary operator.'
    EJP, May 10, 2010
    #7
  8. Ryan Chan

    Lew Guest

    EJP wrote:
    > Thanks, so I should have said 'Neither are operands other than those
    > around a single binary or ternary operator.'


    The operands in the expression

    a + b * c / f

    are evaluated left to right.

    What you said is true but a bit misleading.

    --
    Lew
    Lew, May 10, 2010
    #8
  9. Ryan Chan

    Roedy Green Guest

    On Sun, 09 May 2010 01:20:02 -0400, Lew <> wrote,
    quoted or indirectly quoted someone who said :

    >"The Java programming language guarantees that the operands of operators
    >appear to be evaluated in a specific evaluation order, namely, from left to
    >right."


    see http://mindprod.com/jgloss/precedence.html

    There are left and right associations.

    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    What is the point of a surveillance camera with insufficient resolution to identify culprits?
    Roedy Green, May 10, 2010
    #9
  10. Ryan Chan

    EJP Guest

    On 10/05/2010 2:33 PM, Lew wrote:
    > EJP wrote:
    >> Thanks, so I should have said 'Neither are operands other than those
    >> around a single binary or ternary operator.'

    >
    > The operands in the expression
    >
    > a + b * c / f
    >
    > are evaluated left to right.


    There's nothing in the JLS that actually says that. It says in effect
    the operands *of an operator* are evaluated left-to-right.
    EJP, May 11, 2010
    #10
  11. Ryan Chan

    EJP Guest

    Sorry ignore previous posted in error.
    EJP, May 11, 2010
    #11
  12. Ryan Chan

    Lew Guest

    Lew wrote:
    >> The operands in the expression
    >>
    >> a + b * c / f
    >>
    >> are evaluated left to right.


    EJP wrote:
    > There's nothing in the JLS that actually says that. It says in effect
    > the operands *of an operator* are evaluated left-to-right.


    Of course it says that, in the quote I cited upthread (s. 15.7.1):
    "The left-hand operand of a binary operator appears to be fully evaluated
    before any part of the right-hand operand is evaluated."

    'a' appears to be fully evaluated before '(b * c / f)'.

    'b' before '(c / f)'.

    'c' before 'f'.

    Hence 'a' before 'b' before 'c' before 'f'.

    --
    Lew
    Lew, May 11, 2010
    #12
  13. Ryan Chan

    EJP Guest

    Sorry, agreed. I was recently dealing with a claim that all operands are
    evaluated left to right before any operators, and I'm still touchy about
    it ;-)
    EJP, May 12, 2010
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Frauzel

    Operator precedence

    David Frauzel, May 13, 2004, in forum: Perl
    Replies:
    2
    Views:
    527
    Luc Van Hove
    May 17, 2004
  2. kbd

    Operator Precedence

    kbd, Jul 22, 2004, in forum: Java
    Replies:
    10
    Views:
    730
    Roedy Green
    Jul 27, 2004
  3. Master of C++
    Replies:
    4
    Views:
    700
    Karl Heinz Buchegger
    Feb 7, 2005
  4. Richard Bos

    && and || Operator precedence enforcement

    Richard Bos, Jan 19, 2004, in forum: C Programming
    Replies:
    18
    Views:
    586
    Dan Pop
    Jan 20, 2004
  5. siliconwafer

    operator precedence with && and comma

    siliconwafer, Jul 29, 2005, in forum: C Programming
    Replies:
    21
    Views:
    621
Loading...

Share This Page