0
Why final value of I is 0 in following code Int I=0;I=I++; ?
java post increment
3 Answers
+ 12
I = 0
I = I++
Step 1 : I++ returns 0, I becomes 1
Step 2 : Return value (0) is assigned to I
=> I = 0
+ 1
l++ is called as post increment. here the value of l is used first and then incremented.
eg int l=0;
System.out.print(l++);
output is 0
++l is called pre increment. here the value of l is incremented and then used.
eg int l=0;
System.out.print(++l);
output will be 1
+ 1
because if you do l++,program does the operations first then increases value of l.but if you do ++l, program firstly increases value of l then does the operations next.
for example :
1 2
int x = 0; int x = 0;
x = x++; x = ++x;
System.out.println(x); System.out.println(x);
run this code and you will understand what i meant.