4

I have following code in java

int x=5; System.out.println(x++ + ++x); 

The output is 12.

I thought it should be 11.

We have three operators here:

  • + addition
  • ++ (post)
  • ++ (pre)
  • List item

In which order does the above print statement compile?

If I write int x=5; and then ++x, does x==6 or x==5 as I haven't written x=++x. Does the new value get stored in x?

Looking for a way to remember operator precedence in Java,or .NET, just like we have DMAS. Is their any analogy for this too?

4
  • 2
    You do realise that people wouldn't need to ask questions like this if they just stopped doing stupid things, don't you? :-) Seriously, who the heck writes code like x++ + ++x? Commented Mar 18, 2014 at 13:48
  • 1
    @paxdiablo what if in a exam I have to write output of the following statement , through it carry only 1 mark, should I say "who the heck made this question paper" :) Commented Mar 18, 2014 at 13:57
  • @user1765876 In all seriousness, I would (a) answer it and (b) ask the professor why they'd ask a question like this, given that it's something you should never see in the field. But then, I say that from the comfort of post-graduation world, where professors can't affect me anymore. ;) Commented Mar 18, 2014 at 13:59
  • @yshavit I agree we dont see such things in real worlds but such questions are excellent way to check /assure the concepts of a student. Commented Mar 18, 2014 at 14:02

6 Answers 6

5

x++ is equal to 5 but x has become 6. ++x means 6 is incremented by 1 that is ++x is 7. So 5 + 7 = 12 is the correct answer.

Sign up to request clarification or add additional context in comments.

Comments

2

In C and C++ this kind of thing is undefined behaviour (since, in those languages, the + does not sequence the two expressions).

But in Java, it is defined. The evaluation order is from left to right.

It's quite simple: the first expression is x++ which has the value of 5 but increases x to 6.

The second expression is ++x which increases x from 6 to 7 and has the value of 7.

5 + 7 = 12: Done.

Needless to say, this kind of code is not recommended. And a port to C / C++ would be distastrous.

1 Comment

+1 for mentioning it is undefined in c/c++ before some c programmers start getting any ideas. As if c wasn't convoluted enough in its semantics
1

In the line

System.out.println(x++ + ++x); 

first x is 5, so after x++, x will be 6, but the expression will be evaluated before the increment, so you will have:

5 + ++x 

at this moment x is 6, so ++x will first increment x to 7 and then evaluate the expression. At the end you will have:

5 + 7 

which is 12.

Comments

1

Evaluation/increments will occur before the + operator, one after the other

Your println is, step by step:
(x++ + ++x) where x=5
(5++ + ++x) where x=5
(5 + ++x) where x=6
(5 + x) where x=7
(5 + 7)=12

You have 2 increments of x, from 5 to 7, before summing x thus explaining the total of 12.

Comments

1

post increment

During Expression evaluation only pre incremnted value is used.(Post increment operator is used just after completion of expression evaluation) This may help you

 =x++ + ++x =x++ + 6 =6 + 6 =12 

1 Comment

This contradicts other answers (with 5 and 7) and is probably incorrect.
0

The point is that:

x++ increment x AFTER the assignment in the expression. ++ x increment x BEFORE the assignment in the expression.

So, in your example you actually do 5 + 7. 5 Because the increment is done after the evaluation, so x become 6. 7 because the increment is done before the evaluation, so x become 7.

Comments