• 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

Unexpected output

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I ran this piece of code and the result was 0 when the expected result should be 1. I made an equivalent program in C and executed it and the result was, 1, as expected. Why then is the output 0 in the case of java. I checked up on operator precedence but didnt find anything convincing to justify the result.
Could someone please throw some light on this.
Thanks,
Chetan
Code ----->
--------------------------------------------------
public class Inc{
public static void main(String argv[]){
int i =0;
i = i++;
System.out.println(i);
}
}
--------------------------------------------------
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is similar threads one and
two about this discussion.
help it helps
[ June 12, 2003: Message edited by: chi Lin ]
[ June 12, 2003: Message edited by: chi Lin ]
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here the ++ is post increment so we use the current value of "i" which is zero and assign it, after this the increment takes place.
Thus the result.
 
Chet Arora
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chi,
I followed the links you had suggested. The links in turn had other links which are no longer available. However there was a post where one member says this is a bug in the Java VM. Is that acceptable ?

Sonu,
Yeah ur right about the post increment operator. So one would expect the value of i to be finally incremented to 1 before the print statement. But that doesnt happen. It prints 0. Like i said...i tried this program in C and it came up with the expected answer which is 1 and not 0 (like in Java). And thats where the problem lies...
Regards,
Chetan
[ June 12, 2003: Message edited by: Chet Arora ]
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chet,
First, I don't consider this as VM bug.
Second, one of the ranchers (Maha anna) did
present a good approach about this issue. Maybe it is too long ago so the link is gone.
I managed to find another thread
for this issue.

please pay attention to Satay5's post on 5/2/00 9:07 AM ... similar experiment has been conducted by Satay5.
HTH
[ June 13, 2003: Message edited by: chi Lin ]
[ June 13, 2003: Message edited by: chi Lin ]
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with chetan's argument.
Consider this
int i = 0;
int j = 0;
j = i++;
//j = 0(1)
System.out.println("i = "+i);
System.out.println("j = "+j);
So first the assingment takes place to 'j' and then 'i' is incremented but not assigned which makes perfect sense in this case.
Now going back to the statement 'i=i++' we can use the same analogy and say that the value on the left hand of the equation is '0' while the value on the right hand of the equation is '1' and this is the current value of i(so in the memory '0' must have been replaced with '1'). hence the output must have been 1
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, the behavior is exactly what the JLS says it should be so there is no bug involved here at all. We are talking about operator precedence.
You need to check the precedence list. Postfix has a higher precedence than assignment. That means that the postfix operator is done before assignment. But, The postfix operator increments the value by one, but returns the current value. In the example, x = x++; if x=0 then x is incremented by 1 to 1 but 0 is returned for the assignment.
Look at: x = x++ + x++;
First x is incremented but 0 is returned. So x = 1 even though 0 is returned. So now we have:
x = 0 + x++; // x = 1
Now we incrment x again so x is now 2, but we return 1. The assignemnt statement gets the 1.
So the assignment looks like this:
x = 0 + 1;
End of story.
Second, the behavior you are seeing in C is not documented. Some C compilers will give the same result as Java. You should never code that way in C because the results are undefined.
 
Sudhakar Krishnamurthy
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The postfix operator increments the value by one, but returns the current value. In the example, x = x++; if x=0 then x is incremented by 1 to 1 but 0 is returned for the assignment.


If this is true, then why is the output different when i do x=++x??
TIA
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the prefix operator updates the variable by one before returning the value. The postfix operator updates the variable by one after returning the value.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Thomas Paul:
Because the prefix operator updates the variable by one before returning the value. The postfix operator updates the variable by one after returning the value.


Order of things for x = x++; :
1) return value of variable
2) update variable with ++
3) assign returned value to variable
Order of things for x = ++x; :
1) update variable with ++
2) return value of variable
3) assign returned value to variable
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Thomas,
The explanation is great.......
 
Sudhakar Krishnamurthy
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks...that helps..
reply
    Bookmark Topic Watch Topic
  • New Topic