aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Precedence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Precedence" Watch "Precedence" New topic
Author

Precedence

Bojan Knezovic
Ranch Hand

Joined: Nov 20, 2003
Posts: 90
Can someone shed some light here please, I'm desperate

y is 0!
I thought it would be y=-1 + 0 but it seems to be y=-1 + 1.
How come??
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
It is evaluated left to right:
1. y = x++ (At this point y = -1 and x now equals 0)
2. y = y + ++x (Now y = 0 and x = 1)


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Adrian Pang
Ranch Hand

Joined: Feb 20, 2004
Posts: 40
Just a guess, I think here's what happened:
x=-1
y=x++ + ++ x; // x= -1
x++ has the most precedence, so it becomes:
y= (-1) + ++x; // x = 0
++x is next:
y = (-1) + (1);
therefore the result is 0.
It is interesting to compare this with:
x = -1;
x += x++;
Which will give -2, in my mind the += operator works like:
int tmp = x;
x = tmp + x++;
Hope this helps and the above is correct...
Adrian


SCJP 1.4, SCWCD 1.4, SCBCD 1.3
Bojan Knezovic
Ranch Hand

Joined: Nov 20, 2003
Posts: 90
Originally posted by Michael Morris:
It is evaluated left to right:
1. y = x++ (At this point y = -1 and x now equals 0)
2. y = y + ++x (Now y = 0 and x = 1)


I thought x gets inceremented only AFTER the = operator (for #1, x++), isn't that what's pre and postfix notation all about... Oh well... :roll:
I thank you and Adrian.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Precedence
 
Similar Threads
Need help on animation using GUI Builder.
Probably simple code needed translated into English for me
Increment operator
static block understanding
Postfix Increment/Decrement Operators