• 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

++ Operator's wierd behaviour!

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !!
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A similar one is in this http://www.javaranch.com/ubb/Forum24/HTML/004778.html
Try.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
------------------
Adeesh
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found a related question on the ++ Operator which worth a re-visit:
http://www.javaranch.com/ubb/Forum24/HTML/000063.html
Kevin
 
We find this kind of rampant individuality very disturbing. But not this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic