File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes operator precedence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "operator precedence" Watch "operator precedence" New topic
Author

operator precedence

Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
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.


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10912
    
  12

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

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.


"If the facts don't fit the theory, get new facts" --Albert Einstein
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
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

Joined: Oct 13, 2005
Posts: 36478
    
  16
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

Joined: Nov 06, 2000
Posts: 177
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

Joined: Oct 02, 2003
Posts: 10912
    
  12

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

Joined: Sep 12, 2009
Posts: 316
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 .




SCJP 6.0 96%

(Connecting the Dots ....)
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Thanks for everyone.

I understand the concept now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: operator precedence
 
Similar Threads
Static Initializer Code
Threads - How to approach such a question?
how this uotput comes?
Need help to trace java code
a question on operator precedence