This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Increment operation question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Increment operation question" Watch "Increment operation question" New topic

Increment operation question

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
I was reading over some posts in the SCJP forum and there was a question about the increment operation. Alhough I think I understood the answer there, I came up with a puzzling problem of my own that I cannot figure out.

Consider this code:

the output is:


Although I can't follow the flow to figure out why.

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
marc weber

Joined: Aug 31, 2004
Posts: 11343

i = j++; //i is set to 0, after which j is incremented to 1
j = i++; //j is set to 0, after which i is incremented to 1

"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
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Then why is the output of System.out.println(i) 1 ? Sorry, I'm slow
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
nevermind, I see, i is incremented in the second assignment. j = i++; Told you I was slow.
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Well this is such a simple solution that Java works on Stack Memory for local Variables thus the value of J is still 0. I hope you can understand now.

The Best way to predict your future is to create it - Every great individual common man
I agree. Here's the link:
subject: Increment operation question
jQuery in Action, 3rd edition