8 Replies Latest reply: May 31, 2012 4:25 PM by EJP RSS

    Operator Precedence for Increment Operator

    UdayaShankaraGandhi
      Hi Guys,

      When i am trying an example to work with Increment Operator (either pre-increment or post-increment operator), i studied in one book that associativity of increment operator will be from right to left. And i tried one example to confirm, but it seems associativity is working from left to right.

      Here is my example
      public class RightToLeft 
      {
           public static void main(String[] args) 
           {
                int i = 5;
                int j = ++i * i++;
                System.out.println(" i value :"+i);
                System.out.println(" j value :"+j);
           }
      }
      
      Ouput : i value : 7
                  j value : 36 
      Here, if java is working with right to left associativity in handling increment operator the value of j would be 35, but it is giving 36 by evaluating the expression from left to right.

      Can you please let me know if i am thinking in a wrong way.

      Thanks,
      Uday
        • 1. Re: Operator Precedence for Increment Operator
          gimbal2
          Perhaps you should just put it in simpler terms.

          ++i: increment first, then use incremented value in expression
          i++: use original value in expression first, then increment it

          Does that help to understand what is going on?

          Edited by: gimbal2 on May 31, 2012 4:02 AM : fix typo
          • 2. Re: Operator Precedence for Increment Operator
            452196
            The increment operators are monadic, and monadic operators bind more tightly than any diadic. The associative property doesn't apply to monadic operators.

            So you get the equivalent of (++i) * (i++). The associativity relates to the * operator, but it only applies if you have more than one diadic operator of the same precedence.

            Associativity determines the order in which, say " i + j + k" is evaluated. Whether as (i + j) + k or i + (j + k). (Either way it's still evaluated left to right).

            Edited by: malcolmmc on 31-May-2012 12:00
            • 3. Re: Operator Precedence for Increment Operator
              940622
              In your case...

              int i = 5;
              int j = ++i * i++;

              When he went "++i" the "i" receive "6". As the "i" received "6" then next operation will utilize "6".

              int j = 6 * 6;

              As "i++" increment after the operation then "i" receive "7" after to execute operation.
              • 4. Re: Operator Precedence for Increment Operator
                UdayaShankaraGandhi
                Hi
                Thank you for your info. Can you please elaborate what are monadic and dyadic operators? If possible please give any web link which is discussing on these operators.

                Thanks,
                Uday
                • 5. Re: Operator Precedence for Increment Operator
                  796440
                  Udaya Shankara Gandhi wrote:
                  Hi
                  Thank you for your info. Can you please elaborate what are monadic and dyadic operators? If possible please give any web link which is discussing on these operators.
                  http://lmgtfy.com/?q=monadic+operator
                  • 6. Re: Operator Precedence for Increment Operator
                    EJP
                    The usual terminology is 'binary', meaning it takes two operands, and 'unary', meaning it takes one.
                    • 7. Re: Operator Precedence for Increment Operator
                      rp0428
                      >
                      i studied in one book that associativity of increment operator will be from right to left
                      . . .
                      If possible please give any web link which is discussing on these operators.
                      >
                      Don't know where you read that but for Java the definitive source is the Java Language Specification
                      http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.15

                      See Chapter 15 Expressions
                      >
                      This chapter specifies the meanings of expressions and the rules for their evaluation.
                      >
                      Sections in the chapter specify the requirements - note the word 'guarantees' in the quoted text - see 15.7.1
                      >
                      15.7. Evaluation Order
                      The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.
                      . . .
                      15.7.1. Evaluate Left-Hand Operand First
                      The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.

                      If the operator is a compound-assignment operator (§15.26.2), then evaluation of
                      the left-hand operand includes both remembering the variable that the left-hand
                      operand denotes and fetching and saving that variable's value for use in the implied
                      binary operation.
                      . . .
                      15.7.2. Evaluate Operands before Operation
                      The Java programming language guarantees that every operand of an operator (except the conditional operators &&, ||, and ? :) appears to be fully evaluated before any part of the operation itself is performed.
                      . . .
                      15.7.3. Evaluation Respects Parentheses and Precedence
                      The Java programming language respects the order of evaluation indicated explicitly by parentheses and implicitly by operator precedence.
                      • 8. Re: Operator Precedence for Increment Operator
                        EJP
                        He is is talking about associativity: you are talking about evaluation order of operands. They aren't the same thing. It's not clear to me that he knows the difference either, since he speaks of associativity but his title is 'operator precedence', which is yet a third topic, but there is a difference, and he's wrong. The only operator that is right-associative in any language I'm aware of is the exponentiation operator, and Java doesn't have one. a**b**c should be evaluated as a**(b**c) to conform with the rules of mathematics.