aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Post increment operator faux pas? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Post increment operator faux pas?" Watch "Post increment operator faux pas?" New topic
Author

Post increment operator faux pas?

Gustavo Quiroz
Greenhorn

Joined: Nov 08, 2004
Posts: 12
Hello. Can anyone please explain to me why Code Fragment I yields Output I while Code Fragment II yields Output II?

//Code Fragment I
int i=0;
i = i++;
System.out.println("i="+i);

//Output I
i=0

//Code Fragment II
int i=0, a=0;
a = i++;
System.out.println("a="+a);
System.out.println("i="+i);

//Output II
a=0
i=1

It seems than the JVM doesn't execute the post increment on the variable "i", but only when the LVALUE (variable on the left side of the assignment operator) is the varible "i" itself. Why?

Thanks in advance.

Gustavo Quiroz
Lima - Peru


=============================<br />Gustavo Quiroz<br /> <br />CSM <br />IC SOA Associate<br />ICAD (RAD 6.0)<br />ICED (WSAD V5.0)<br />ICSD (MQ V5.3)<br />ICSD (XML/WSAD V5.0/MQ V5.3/Portal V5.1)<br />ICSA (WAS ND V6.0/WAS V5.0/Portal V5.1)<br />ICAD (WSAD V5.0)<br />ICAD (DB2 V8.1)<br />ICDA (DB2 V8.1/DB2 V9)<br />SCJP (V1.4)<br />=============================
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You may find Corey's Blog Article useful.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
i = i++; has been discussed many times.

It breaks down to this:
1- evaluate i, its current value is 0 so, 0 will be assigned.
2- increment i
3- assign 0 to i
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Post increment operator faux pas?