Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
The moose likes Beginning Java and the fly likes The question about : j=j++ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The question about : j=j++" Watch "The question about : j=j++" New topic

The question about : j=j++

Jonh yang

Joined: Feb 18, 2004
Posts: 1
Hi.. i am really a newbie, just a java beginner.
I need some help.
After executing the statement:j = j ++;
i found that the value of "j" wouldn't increase...but why ?
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 31683

Either of the following will increment j:

When using j = j++, it is adding one to j and overwriting it with the original value.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9056
Almost. If you look at the output of javap, I think you will see that in the case of j = j++, j is incremented after the value of 'j' is stored. On the other hand, in the case of j = ++j, j is incremented first and then the resulting value is stored in 'j'.

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
check out this thread:
post increment

How simple does it have to be???
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
what you're doing is effectivel this:

though the compiler may optimise the whole thing away
j = ++j; would lead to

Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

If you want a specific anser you can always look in the JLS. I believe java behaves differently that c in this respect.
Jos� Alberto Mu�iz Navarro
Ranch Hand

Joined: Oct 27, 2003
Posts: 33
Yeah. in Microsoft's C compiler you get an output of 1 when you do

However, you get an output of 0 when you do

jQuery in Action, 2nd edition
subject: The question about : j=j++