Coding is easy; it’s maintaining other people’s code that really gets on my nerve. Just the other day I came across something that made me laugh in anguish! As usual looking for your help!
package com.twister;
public class TooMuchAdd {
public static void main(String[] args) {
int i = 5, j;
j = i++ + i++;
System.out.println("Value of i : " + i);
System.out.println("Value of j : " + j);
}
}
Got an answer? Why don’t you leave it here.
It is easy :)
ReplyDeleteThe outputs are:
Value of i : 7
Value of j : 11
Thanks my friend.
your friend: "Turki.27" :)
Was'nt that simple,
ReplyDeletei=7 and j=11
I think....
ReplyDeleteLine 6 would evaluate as j = 5 + 5;
Therefore, in the end i = 7, j = 10.
If you wanted the final value of j to be 13 you would have to rewrite line 6 as:
j = ++i + ++i;
The latter forces the increment operation to happen before the assignment.
i=12,j=11
ReplyDeletei=7,
ReplyDelete1.i=5
2.i++,so i=6
3.i++,so i=7
j=11
j=i++ + i++;
means
j=5+6;
j=(initial i)+((i+1))
then i becomes (i+1)+1
Value of i : 7
ReplyDeleteValue of j : 11