This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Any body explanation why ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Any body explanation why ..." Watch "Any body explanation why ..." New topic

Any body explanation why ...

Pradeep Kalyan

Joined: Mar 05, 2006
Posts: 6
does the following program return 10 as output. In C or C++, the same code returns 11.

i = i++ ;
System.out.println("output i: " + i);

Does any body explain what's going on here?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I assume that i is initially 10. The right-hand side is a post-increment, so its value as an expression is the value of i before the increment. So i is incremented by 1, but set back to 10.
marc weber

Joined: Aug 31, 2004
Posts: 11343

I'll just add a little bit of detail to what Keith said (again assuming that i is 10).

i = i++; is a simple assignment, so first the "left-hand operand is evaluated to produce a variable." In this case, i. (See JLS - 15.26.1.)

Next, the right hand-hand operand is evaluated. With a postfix operator, the term i++ evaluates to 10, after which i is incremented to 11. (See JLS - 15.14.2.)

Finally, because the right-hand operand evaluated to 10, the value 10 is stored into the varaible i.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245

does nothing (at least in siglethreaded program). In both Java and C.
Shaliey Gowtham
Ranch Hand

Joined: Mar 20, 2006
Posts: 104
Please have a look at assembly level
intially i = 10;
i = i++ can be split as
load i on stack; \\top = 10
increment the variable locally for i++; \\ i = 11
now store the value on top of stack into i; \\ i = 10;
So the final result is i = 10;
For i++ instruction the value is not brought to the stack,but
incremented at the location through inc 1

But if i = i + 1, then
load i on stack; \\ top = 10
add one to top of stack; \\top = 11 (10+1)
store the value on top of stack into i; \\ i = 11

Hope you are clear
ashok ch

Joined: Feb 21, 2006
Posts: 11
see this
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41133
Hello "ashok"-

Welcome to JavaRanch.

On your way in you may have missed that JavaRanch has a policy on display names, and yours does not comply with it - please adjust it accordingly, which you can do right here. Thanks for your prompt attention to this matter.

Enjoy your time here.

Ping & DNS - my free Android networking tools app
dai jiansong

Joined: Jan 13, 2006
Posts: 7
what a good example!

come on baby
I agree. Here's the link:
subject: Any body explanation why ...
Similar Threads
Explain please
Prestigious C++ certification
HFSJ : page:553 Q:9
this and super
problem with display table pagination and sorting