Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# ++ Operator's wierd behaviour!

Kav Bop
Greenhorn
Posts: 7
Hi!
Can anybody tell me why the incremental operator behaves the way it does in the following program.
According to me, following are the steps :
1. i is an int variable, initialised to 0.
2. Next, i (on the left side) is assigned value 0 & then i (on the right hand side) is incremented, since here ++ is a post incremental operator.
3. The value printed for i is 0 and not 1.
Since i is the same, the value should be 1 & not 0.
Can anybody explain!!!
class IncrementalOperator
{
public static void main(String args[])
{
int i = 0;
i = i++;
System.out.println(i);
}
}

Kalpesh Pandya
Greenhorn
Posts: 14
Hi,
if ++/-- operators are placed on the left of an expression, then the value of expression is modified before it takes part in the rest of operation (pre-increment / decrement). If they are placed on the right of an expression, original value of the expression is used in the calcualtion and ++/-- only gets evaluated after calculation.(post incr/decr)
Hope this helps !!

lakshmi nair
Ranch Hand
Posts: 63
A similar one is in this http://www.javaranch.com/ubb/Forum24/HTML/004778.html
Try.

Bin Zhao
Ranch Hand
Posts: 73
You can think in this way:
first i++ is evaluated and the value of this expression is 0.
Then the value is assigned to i,so i got the value 0.

umang bhartia
Ranch Hand
Posts: 60
hi,
As u r using post increment operator ++, that is the reason it is first get assigned to i and then the value of right hand side i is incremented, so u get the output as 0.
Try this out:
class IncrementalOperator
{
public static void main(String args[])
{
int i = 0;
int j=0;
j = ++i;

System.out.println(j);
}
}
the output will b 1
i hope its clear???

UPENDRA KANE
Greenhorn
Posts: 24
None of the replies are clear or do not make sence to me.
I completely agree with Kav's explaination given at the begining, but, the result does not make sence.
Umang, if you use
int J=0, I=0;
J=I++, then
J is printed as 0 & I is printed as 1. This is perfectly logical.Because, I's value is assigned to J before increamenting hence J is 0 & then I is increamented hence, I is 1.
But when I=I++ is done, using above explaination, I's value is assigned to I before increamenting (i.e. 0) then I is increamented which should be 1??
I am yet to check the other post referred by Laxmi. Hope there are some explanations more clear.
I remains 0?

Lahcen Mannou
Greenhorn
Posts: 7
I think this "strange behaviour" may be explained like this :
When evaluationg the expression "i = i++", the following steps are performed :
1. first evalaute i++ expression. The result is 0.
2. then, (because the ++ operator has higher precedence than =),
increment i, so now i=1
3. finally affect the value of the expression (wich is 0)to i. i was 1 (after step 2), but now it becomes 0 !
I hope this explanation is OK. Any comment ?
Lahcen.

George Richardson
Greenhorn
Posts: 1
I think the reason for the unexpected behavior here is due to operator precedence as follows:
1. "i++" is evaluated because "++" is top of precedence table.
"i" will now equal 1 but the "i++" expression as a whole is still 0 as it is a post increment;

2. the value of the "i++" (0) expression prior to post increment is assigned to i (ie i is now 0)
because "=" is at the bottom of the preference table.
what it comes down to is that although the "++" is a post increment it would appear that Java is actually doing the "++" first (in operator preference order) but then assigning the old value prior to "++" back to "i" thereby overwriting the incremented value.
I think the reply by Lahcen Mannou says the same thing as me but in different language.

Shobhna
Greenhorn
Posts: 1
In your code you are assiging the value first (which is i =0 )and then incremneting i,Hope this modified version of code will clear your doubt...
class IncrementalOperator
{
public static void main(String args[])
{
int i = 0,j = 0;
//i = i++;
j = i++;
System.out.println("i = " + i);
System.out.println("j = " + j);
}
}

rahul pathak
Greenhorn
Posts: 11
the best way to think about is to resolve all the parameters so as assignment takes place last it is resolved to 0 .
though i++ is supposed to happen after everything(it does )it is resolved earlier

shanks iyer
Ranch Hand
Posts: 47
Hi,
I think there is an excellent explantion discussion on maha's page and that will clear all your doubts coz her explanation the topic is operators and assignments ++ hairy behaviour i think that should help.
Regards,
Shankar.

Kav Bop
Greenhorn
Posts: 7
Thanks everybody for your replies, however none of the replies has really cleared my doubt regarding the behaviour of ++ operator.
The tricky thing is, the variable i is on both sides, hence, logically speaking, i is assigned the value 0 & then i is incremented...However, the end value of i is not 1 but 0.

Greenhorn
Posts: 11
I think this can be explained like this
in i = i++;
there are two operators
= and (postfix)++
since (postfix)++ has higher precedence than = it will be evaluated first, so i is 1 now. But since it is postfix operator its operand's old value will be used in expression it appears in.
keep in mind that i is still 1 only its old value before (postfix)++ is used in expression (i.e assignment). As a result of assignment (i = 0) i's value 1 is replaced with 0.
I'm still new to Java, so pls comment on this explanation.

Amit Saini
Greenhorn
Posts: 5
Hi all,
i=expr;
-the value of the expr is assigned to i;
-now the assignment operator is at the last of precedence table
therefore whatever be the expression, the value of that expression will be assigned to i, as the last step of evaluation.
-now let us dissect the expression>
i=0;
i=i++;
the value of i comes out to be 0 as we all know.
this is how it all works.
++ has higher precedence.
i++ will increment the value of i but u see since it is a post increment operator the initial value of i i.e 0 is somehow magically stored in the expression..
i=(i++) as i=(0); //note i is incremented to 1
now , the final evaluation is:
i=0; // even at this moment i was 1;
but after the exec. of the above statement i becomes 0;
i hope it is clear

Kavita Bopardikar
Greenhorn
Posts: 13
Hi everybody,
Thanks a lot for ur replies.
I think I understood it somewhat.
Thanks once again!

Originally posted by Amit Saini:
Hi all,
i=expr;
-the value of the expr is assigned to i;
-now the assignment operator is at the last of precedence table
therefore whatever be the expression, the value of that expression will be assigned to i, as the last step of evaluation.
-now let us dissect the expression>
i=0;
i=i++;
the value of i comes out to be 0 as we all know.
this is how it all works.
++ has higher precedence.
i++ will increment the value of i but u see since it is a post increment operator the initial value of i i.e 0 is somehow magically stored in the expression..
i=(i++) as i=(0); //note i is incremented to 1
now , the final evaluation is:
i=0; // even at this moment i was 1;
but after the exec. of the above statement i becomes 0;
i hope it is clear

Greenhorn
Posts: 28
I think the reply by George is a perfect explanation.
The value of i after increment is 1 which is stored in temporary location. For evalutaing expression value is 0 since it is postincrement. This value is assigned again to i. And the value 1 is discarded.
------------------

mohit joshi
Ranch Hand
Posts: 243
some guys here are stating that evaluation order depends on the precedence of operators. try to answer the following qustion without compiling. what will be the output of following code?

y = fx1()+ fx2() * fx3();

int fx1(){return 1;System.out.println("fx1");}
int fx1(){return 2;System.out.println("fx2");}
int fx1(){return 3;System.out.println("fx3");}

Kavita Bopardikar
Greenhorn
Posts: 13
Couldn't resist it, so compiled & tried it.
At first glance, i found out that the code will give compilation errors, since the statements after "return" are unreacheable code. Hence, first of all, the return statement must be the last statement.
Secondly, I think since this is a function call, functions will be evaluated in the order in which they are called. Hence, considering that following is the revised code, the output after the function calls will be fx1 fx2 fx3.
class EvaluationOrder
{
public static void main(String args[])
{
EvaluationOrder eo = new EvaluationOrder();
int y = eo.fx1()+ eo.fx2() * eo.fx3();
// int z = 4 + 4 * 3;
// System.out.println(z);
}
int fx1() { System.out.println("fx1"); return 1; }
int fx2() { System.out.println("fx2"); return 2; }
int fx3() { System.out.println("fx3"); return 3; }
}
However, the operator precedence will be considered for the commented statement where z is assigned some value of an expression.

Eric Boehme
Greenhorn
Posts: 3
Simple explanation:
int i = 0;
i = i++;
System.out.println(i);
Step through it:
i = 0 by assignment
then i is assigned i++
the increment in post, meaining it happens after evaluation
so, i is assigned 0 again, because i++ is 0
After the assignment i would thne increment.
Consider example below:
int i = 0;
j = i++;
k = i;
j = 0, post increment, i is still zero, not incremented yet
k = 1, i is incremented before i is assigned to k

Hope this helps

Kevin Yip
Ranch Hand
Posts: 110
I found a related question on the ++ Operator which worth a re-visit:
http://www.javaranch.com/ubb/Forum24/HTML/000063.html
Kevin

 It is sorta covered in the JavaRanch Style Guide.