This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes i=i++; Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "i=i++;" Watch "i=i++;" New topic
Author

i=i++;

Shivaprasad P Kanaganahallimath
Ranch Hand

Joined: Sep 25, 2006
Posts: 48
public static void main(String [] args){
int i=9;
i=++i;
i=i++;
int j=i;
System.out.println("i value is:"+i+"j value is:"+j);
...
the output is
i value is:10 j value is:10
---------------------------------------------------------------------------

but the following gives different answer
public static void main(String [] args){
int i=9;
i=++i;
int p=i++;
int j=p;
System.out.println("i value is:"+i+"j value is:"+j);

i value is:11 j value is:10


could you guys explain this please.


Shiva Prasad P.K.
Costa lamona
Ranch Hand

Joined: Sep 24, 2006
Posts: 102
Hi

that is because "prefix and postfix" ++, does not differ from each other until the identifier is involved in expression, for instance

produce output 1
but

produce output 0
and

produce output 1


SCJP 5
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: i=i++;
 
Similar Threads
String label?
help
why i=i++; doesn't get incremented
Compilation problem with if condition
i=i++;