• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Post increment operator faux pas?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



The post increment is done after the RVALUE is calculated (it is a bit more complex, but it is okay here).

Hence, i is incremented, after i is loaded. However, it is then set back to zero when it is assigned. Remember, the increment is post, the calculated RVALUE is zero, and is assigned back to i.

Henry
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a complicated question. The same code will result in different values with different languages (e.g. c/c++).

In java, basically there is a three step process that takes place when there is a postfix operator used in an assignment statement.

step 1. The original value is saved.
step 2. The postfix operation takes place.
step 3. the original value is assigned.

This is not what the programmer is expecting. Most programmers would expect steps 2 and 3 to be reversed.

Let's take a look at this example:


step 1: store the original value of x (TEMP = 0)
step 2: increment x (x = x + 1)
step 3: assign the original value to y (Y = TEMP)

now, let's look at your example:



step 1: store the original value of i (TEMP = 0)
step 2: increment i ( i = i + 1 )
step 3: store the original value to i (i = TEMP)


It is correct per the JAVA language specs, however it is not intuitive and I for one would like to see it behave in a manner more closly to that of C++.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by jefff willis:
I for one would like to see it behave in a manner more closly to that of C++.



The ANSI C/C++ standards have nothing to say about what the legal result of such a statement is: it's formally "undefined." That means that the Java behavior is perfectly legal C/C++ behavior. So is your version with the order of execution reversed. So are a half-dozen other possibilities. So, technically, is a core dump (although a compiler vendor that implemented this wouldn't stay in business long!)

One of the neat things about Java as compared to some other less-well-defined languages is that in Java, you can try something and see what happens, then correctly believe that the same thing will always happen. In C/C++, you have to be acutely aware of what the standards say, and you can't trust an implementation to be normative.
 
jefff willis
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


One of the neat things about Java as compared to some other less-well-defined languages is that in Java, you can try something and see what happens, then correctly believe that the same thing will always happen. In C/C++, you have to be acutely aware of what the standards say, and you can't trust an implementation to be normative.



I agree. I don't think JAVA is wrong here. But I do think it is not delivering the results that are intuitive to programmers in general.

Perhaps that is because so many JAVA programmers have learned C/C++ before they learned JAVA.

Anyway, are you saying here that you think this operation could possibly result in different results given different C/C++ compilers?

If so, then that is unacceptable behavior for C/C++.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by jefff willis:

Anyway, are you saying here that you think this operation could possibly result in different results given different C/C++ compilers?

If so, then that is unacceptable behavior for C/C++.



Yes, that's exactly what I'm saying. Acceptable or not, that's the way it is and has always been.
 
reply
    Bookmark Topic Watch Topic
  • New Topic