File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Java variables..strange!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java variables..strange!!" Watch "Java variables..strange!!" New topic

Java variables..strange!!


Joined: May 29, 2001
Posts: 6
there is a problem..i am having a method in which ..
int i=0;
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..
Jyotsna Clarkin
Ranch Hand

Joined: Jan 26, 2001
Posts: 158
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)
'i' will always evaluate to zero- every time.
You can test this by changing your code sightly
int i = 0;
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,

Joined: May 29, 2001
Posts: 6
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).]
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

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
I agree. Here's the link:
subject: Java variables..strange!!
It's not a secret anymore!