• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

operator precedence

 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have simple question.

Below is the part of my code

int x=4;

x=x + ++x;

It shows the value of x as 9.

My question is as per the precedence rule ++x will be executed first so x=5

Then when the express x+ 5 is evaluated it agains take the old value of x next to = sign instead of new value 5 and shows the output as 9.

why the output is not 10?

Please explain.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12149
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when parsing the right-hand-side of the equals sign, you parse the tokens in order. first, you parse the 'x' token. it is 4. you remember that.

then you parse the '+' token, which is saved for the moment.

then you parse the ++x token. this says "increment x, then get that value". so x is 4, you increment it to 5, and return the 5. you now us the remembered value of 4, the +, and the value of 5 in your addition.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in your expression you are using the plus operator for both expressions, which evaluates from left to right.

x = x + ++x = 9 because (x = 4) + (++x = 5) = 9

x = ++x + x = 10 because (++x = 5) + (x =5 now) = 10

'Hunter.
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am still confused.

Let me know if understood it correctly.

Java first replaces the variable with values before it do any operation ,so the above expression would become

x=4 + ++4 (internally) then it applies the operator precedence rules so ++4 is evaluated to 5 and then + operator is evaluated?

am i correct?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know whether you replace the x with 4. You would have to write a method including that code, then compile it, then examine it with the javap tool and the -c option. See what the bytecode says.

I think you have got the precedences worked out, however.
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When coding, I recommend never using the "++" operator in an expression. It's just too confusing, and easy to misunderstand, both for the programmer and for the person maintaining the code.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12149
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to be a little careful here, I think. for example...

int x = 4;
x = ++x + x

would (i think) give 10.

 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya it wuld be 10.

Because remember ++x and x++ both changes the value of orignal x. whereaas x+1 and 1+x do nat changes x .



 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for everyone.

I understand the concept now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic