GeeCON Prague 2014*
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


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The question about : j=j++" Watch "The question about : j=j++" New topic
Author

The question about : j=j++

Jonh yang
Greenhorn

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
Marshal

Joined: May 26, 2003
Posts: 30586
    
154

Either of the following will increment j:

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


[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
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
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'.


JavaBeginnersFaq
"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
davy


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


42
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

 
GeeCON Prague 2014
 
subject: The question about : j=j++