File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes i++ unary operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "i++ unary operator" Watch "i++ unary operator" New topic

i++ unary operator

Shubh Bhat
Ranch Hand

Joined: Sep 08, 2002
Posts: 32
Sorry I posted this before in the wrong forum
1. Why would this code print 11 and not 12?
2. Can you refer me to a good site which explain exception handling in detail? :roll:

[ Jess added UBB [CODE] tags to preserve whitespace ]
[ September 08, 2002: Message edited by: Jessica Sant ]

~-<br />Doing my best this time and everytime.<br />-- Me
Jessica Sant

Joined: Oct 17, 2001
Posts: 4313

"Dil Cheese"
Welcome to the JavaRanch!
Please adjust your displayed name to meet the JavaRanch Naming Policy. It should not be obviously fictious, and preferrably your real name.
You can change it here.
Thanks, and again welcome to the JavaRanch!
Now for your question, first thing to remember:
pre-increment, ++i increment first then use the variable
post-increment: i++ use the variable then increment it.
So now lets look at your code

line 1: self-explanitory, i = 10
line 2: As always evaluate the right side first, pre-increment so RHS = 11, then set the LHS (i) to 11.
line 3: Again evalue the right side first, post-incremnt so RHS = 11, then set LHS (i) to 11, then RHS is incremented... but no matter b/c LHS is already set to 11.
line 4: print the value of i --> 11
[ September 08, 2002: Message edited by: Jessica Sant ]

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Shubh Bhat
Ranch Hand

Joined: Sep 08, 2002
Posts: 32
well, I really don't see it, because RHS is the same variable as LHS and so if i is incremented in the RHS after the assignment, still it should point to the same memory location as LHS (since they are the same) and try to increment it.
I really don't get it at all if anybody could explain it, it would be great!
class TrickTest {
public static void main ( String args [ ] ) {
int k = 0;
int i = 10 ; // line 1
i = ++i ; // line 2
i = i++ ; // line 3
i = k++;
System . out . println ( i ); // line 4
System . out . println ( k ); // line 5

Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
i = i++ ;
a) i = 11++ ;
first i is evaluated
b) 12 = 11 ;
second the variable i is incremented
c) 11
third the assignment overrides the previouly calculated value

hope it helps
[ September 08, 2002: Message edited by: Jose Botella ]

SCJP2. Please Indent your code using UBB Code
Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 267
I think I understand Shubh's confusion as I am now confused too after reading your explanation, no offense. This is how I understood your reply:
i=11++; (i is evaluated)
12=11++; (i is incremented)
11=11++ (assignment overrides the previously calculated value)
The way it is laid out here the variable reverts back to its previous value yet it is the same variable. This might be the underlying point to the problem but its just left me unsure as the variable is on both sides. Initially I thought 11 because the way I think it through is that for post-increment operators such as ++ you do the assignment and then increment. Since its the same variable I can see why Shubh would get confused wiht the result For example in this code he supplied :

After line 2: i = 11 because you increment first then assign
After line 3: i = ll still because you assign first(this is my initial understanding, the curve ball now is Shubh's point of view...same variable used on both sides, post-increment, assign first so i = 11, increment, i = 12, Is this where the value of the variable is overriden with the previous value of 11? Is this a quirk of the language?)
The end result, due to the assignment and post-increment of k, is i=0 and k = 1. (Unless the previous post is true, and since its the bartender it probably is, which would then make them both 0).
Shubh Bhat
Ranch Hand

Joined: Sep 08, 2002
Posts: 32
ok my confusion is exactly what Matt described
i =10;
i = ++i; // value : 10+1 then assignment
i = i++; // since ++ is done after the assignment so I take that i = 11 and then i+1 (which is 12) after the assignment. Now what is happening internally to give us 11 back? Is it first incrementing i storing in some temp in the memory and then assigning the value to i and some how discarding the temp for some strange reason because it thinks its already assigned it something(hey, I am just trying to make up something)?
Sorry still in a confused state
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
Hello Shubh,
Here's how we could explain the evaluation order for
i = 10;
i = ++i;
i = i++; // line 1

For line 1
First put the value of i ( i.e 11 ) in the expression , then increment it & store it back in i ( now i == 12 ) . Finally comes the assignment wherein lies the catch. We already have put the value of i ( before incrementing i.e. 11 ) into the expression & hence the output is 11 .
The catch lies in the evaluation order. See JLS 15.7 for somemore insight. Good code exaples in there.
Correct me if I am wrong
Ashish H.
Vin Kris
Ranch Hand

Joined: Jun 17, 2002
Posts: 154
Shubh, This is what i understood from the JLS.
In a post-increment operation, the original value of the variable is always stored.
Now, comming to the expression, i = i++;
The original value is first stored. Then i is incremented. But the expression is not yet evaluated. To complete the expression, the stored value of i is used. Therefore, the final value of i is the original value.
Shubh Bhat
Ranch Hand

Joined: Sep 08, 2002
Posts: 32
Vin, when you say "stored" I am assuming in some temp. memory space...if that is the case your explanation is perfect, I can visualize that!!!
if my assumption is wrong please let me know.
1. Links to JLS
2. JCP mock exams(good/hard ones)
Thanks for all your help!
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
Good resource on the ++ operator:

[Blog] [Blogroll] [My Reviews] My Linked In
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
I read the claims that Java is easy to learn. But when it comes to these kind of tricks, it seems that it is unneccessarily complicated. I can not see any practical situation where a real world problem will suit this piece of code....
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
My reply wasn't to be consiered literally, but to help you to visualize, and remember, what's is going on.
The output of javap -c Test can be studied literally. And it shows a sequence of actions similar to what I wanted to summarize graphically in my previous post.

Line 0 sets the operand stack to to 0.
Line 1 store the operand stack into the first local variable. That is i = 0;
Line 2 loads the value of i into the operand stack.
Line 3 increments the local variable i in 1. However the operand stack is unmodified by this operation. The operand stack is still 0, though i is now 1.
Line 6 store the operand stack into the variable. He is to blame of the result.
Shubh Bhat
Ranch Hand

Joined: Sep 08, 2002
Posts: 32
great! exactly what I was looking for!
Thanks a lot Jose, made my day!
I agree. Here's the link:
subject: i++ unary operator
It's not a secret anymore!