This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
Answer to your question....
Posted by ruchi and hema on November 12, 2000 at 5:54 AM
hello.... it is very easy to understand this concept.... in case of post increment(a++)..the value of 'a' will be assigned first and later is will incremented...when u say first time a++ this will assign the value of 'a'(i.e 1) to 'a'..and late it will be incremented ..but this is not the case here...The imp thing to remember here is that in case of post increment if arithmetic operators are not provided the value will not be incremented..So the changes of increment r not visible to u.. so here if u change ur code to a=a++ +2 u will see the value to be 3..i.e the value of a will be assigned first and then incremented and later u do post increment on that value without arithmetic operation it will never change..But this is not the case with pre increment..it will remain the same as we do.. in pre increment arithmetic operators r not required...it will change the value before assignment.....hope ur doubts will be] clear.....try out the foll. code.. int a=1; a=a++; System.out.println(a); a=a++ +2; System.out.println(a); o/p.......1 3
Thanks....hema and ruchi
I am not able to understand why all the printed answers for a is 1 while it is supposed to increase > class test3 > { > public static void main(String s[]) > { > int a = 1; > System.out.println(a); > a=a++; > System.out.println(a); > > a=a++; > a=a++; > System.out.println(a); > > a=++a; > System.out.println(a); > a=++a; > a=++a; > System.out.println(a); > } > }
Replies:
|