3

The output of the following code is declared as "6" when I try to execute this.

When I am trying to think through this, the expression "k += 3 + ++k; should have been evaluated as k = k + (3 + ++k); but in this case the output should have been 7. Looks like it was evaluated as k = k + 3 + ++k; which resulted in 6.

Could someone please explain me why the expression was evaluated as "k + 3 + ++k" instead of " k + (3 + ++k); ?

public class TestClass {

public static int m1(int i){
    return ++i;
}

public static void main(String[] args) {

    int k = m1(args.length);
    k += 3 + ++k;
    System.out.println(k);
}

}

sunsin1985
  • 2,437
  • 5
  • 22
  • 27
  • 2
    All of this is defined in [section 15.7 of the Java Language Specification](http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.7). The relevant section is pretty tedious to read (and thus tedious to explain here), but that's ok, because there's never a need to write code like this in practice ;) – Oliver Charlesworth Aug 03 '13 at 19:44
  • @OliCharlesworth - Thanks for sharing the specifications like. After going through the example "Example 15.26.2-2. Value Of Left-Hand Side Of Compound Assignment Is Saved Before Evaluation Of Right-Hand Side" on the Specifications page, I am able to understand how the expression was evaluated. – sunsin1985 Aug 03 '13 at 19:58

2 Answers2

6

Take a look at the behaviour in JLS - Compound Assignment Operator. I'll quote the relevant two paragraphs here, just for the sake of completeness of the answer:

Otherwise, the value of the left-hand operand is saved and then the right-hand operand is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs.

Otherwise, the saved value of the left-hand variable and the value of the right-hand operand are used to perform the binary operation indicated by the compound assignment operator. If this operation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs.

Emphasis mine.

So, the left hand operand is evaluated first, and it is done only once. And then, the evaluated value of left hand operand, 1 in your case, is added with the result of right hand operand, which turns out to be 5. Hence the result 6.

Community
  • 1
  • 1
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • - Thanks for your reply. I read the following example on the specifications link shared by you. This makes complete sense to me now. The value of the variable "K" was evaluated to "1" (as you have also stated in your answer). After that the right hand operand was evaluated to 5 and added to the value of K after that. Thanks! Example on the JLS: "Example 15.26.2-2. Value Of Left-Hand Side Of Compound Assignment Is Saved Before Evaluation Of Right-Hand Side" – sunsin1985 Aug 03 '13 at 19:56
0

Official Docs on Operators says

All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.

So + is evaluated left-to-right,where as assignment operators are evaluated right to left.

Now you got your answer right ??

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307