• 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

confused about this prefix increment code snippet...

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the first code.
<code>
int i = 0;
i = i++;
System.out.print(i);
</code>
The output is 0.
flow:
i is assigned 0
i is incremented to 1
..but the output shows 0.

Now take a look at this snippet:
<code>
int i = 0;
int y = 0;
y = i++;
System.out.println("y="+y);
System.out.println("i="+i);
</code>
The output is y=0 and x=1.
Any help? Thanks
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The second snippet is fairly straightforward.
When you say y = x++ what you're doing is first assigning y to x and then incrementing x, so y has a zero value and x has one. But it's really strange that the first snippet is acting the way it is.
I tested it in C and it performs ok (answer is 1) but I wonder why it's not working in Java.
Any suggestions?
Sashi
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Just go thru this
http://www.javaranch.com/ubb/Forum24/HTML/000775.html
hope this helps
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Charles,
The last line should be The output is y=0 and i=1
and not The output is y=0 and x=1.
Replace x with i. OK?

Originally posted by Charles Mullins:
Here is the first code.
<code>
int i = 0;
i = i++;
System.out.print(i);
</code>
The output is 0.
flow:
i is assigned 0
i is incremented to 1
..but the output shows 0.

Now take a look at this snippet:
<code>
int i = 0;
int y = 0;
y = i++;
System.out.println("y="+y);
System.out.println("i="+i);
</code>
The output is y=0 and x=1.
Any help? Thanks


 
Charles Mullins
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that is correct. Just typing in the code and didn't cut/paste like I should have. -- thanks
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's because it's post increment.
When you assign a post incremented variable to itself, the value assigned to itself WAS NOT INCREMENTED. It was not incremented until the entire expression is done. And then the original value was already assigned to itself. Hence, the value is unchanged.
 
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 Sasikanth Malladi:
I tested it in C and it performs ok (answer is 1) but I wonder why it's not working in Java. Any suggestions?

This is actually one of the advantages of Java over C. Java's behavior is well-defined and predictable. You may not like the way it works but it is clearly defined in the JLS. C has no rules as to the behavior of this statement, x = x++. It could work one way in some c compilers and a different way in others. The results of that statement are unpredictable in C and should nver be used.
 
Your mind is under my control .... your will is now mine .... read this tiny ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic