Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Java variables..strange!!

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is a problem..i am having a method in which ..
int i=0;
i=i++;
System.out.println(i);
it prints 0 instead of 1, its unlike C++..??
how this problem is actually linked with way of working of JVM, as regards JVM declares space in heap for variables..??
kindly clear my doubt..thanking u..
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the JLS Section.15.14.1 Postfix Increment Operator ++ :


At run time, if evaluation of the operand expression completes
abruptly, then the postfix increment expression completes abruptly for the same reason and no incrementation occurs.
Otherwise, the value 1 is added to the value of the variable and the sum is stored back into the variable. Before the addition, binary numeric promotion (�5.6.2) is performed on the value 1 and the value of the variable.
If necessary, the sum is narrowed by a narrowing primitive conversion (�5.1.3) to the type of the variable before it is stored.
The value of the postfix increment expression is the value of the variable before the new value is stored.


OK This sentence is the important one - remember it
The value of the postfix increment expression is the value of the variable BEFORE the new value is stored.

So the code
int i = 0;
i = i++;
Initial LHS is evaluated to i = 0
The postfix operator has a higher precedence than the assignment
operator so it executes first and 'i' is set to '1'
THEN the assignment ( = ) expression completes,
it assigns the original value of 'i' ... the one BEFORE the postfix increment to the variable 'i', which in this case is once again '0'.
Thus, the postfix value of 'i' is being OVERWRITTEN each time.

So when you write
int i = 0;
i = i++; --(1)
i = i++; --(2)
i = i++; --(3)
System.out.println(i);
'i' will always evaluate to zero- every time.
You can test this by changing your code sightly
int i = 0;
i++;
i++;
i++;
System.out.println(i);
This second example WILL print 3. Since this is a simple postfix
increment the increment value is retained.
The first example was a postfix increment ASSIGNMENT (assigning value back into the original)
Hope this helps,
regards.
Jyotsna
 
NIKHIL JAIN
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot jyotsna for ur so illustrative and wonderful replies!!
i am satisfied with ur answers.

[This message has been edited by NIKHIL JAIN (edited May 30, 2001).]
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is an awesome example of how post-increment operators really and truly work. Anyone that gets this question knows this stuff stone cold.

p.s. No one in my office got it. LOL
 
rubbery bacon. crispy tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic