r/javahelp • u/BeakLee1 • Oct 08 '18
I cant wrap my head around this piece of code
int i=3, j=5;
int k = (i++)+j;
System.out.println(i + " " + j + " " + k);
I feel pretty dumb because it outputs 4 5 8
I was sure it would output 3 5 9
23
Upvotes
3
u/nevergotcompiled Oct 08 '18
Int i is incremented after the sum has been made. If it was ++i then k would be equal to 9 since i was incremented before the sum.
1
u/jhayer_ Oct 31 '18
The ++ is after the i so java executes it at the end after 3 + 5 has been done and this doesn’t effect the answer since i has already been added.
11
u/psikosen Oct 08 '18
Here I'll give you a clue, print after each declaration/statement, that's usually what I do if I get stuck. You'll get it, it not leave a comment.