Back to questions
JuniorTheoryCommon
What does the program print?
public class Main {
public static void main(String[] args) {
int i = 5;
int a = i++;
int b = ++i;
System.out.println(a + " " + b + " " + i);
}
}5 7 7
- Expecting
6 7 7: both forms add one toi, buti++gives the value from before the addition, soais 5. - Expecting
5 6 7:++iadds one before its value is taken, sobgets the new value 7, not 6.
- What value does
jhold afterint j = 5; j = j++;? - Does it matter whether the step of a
forloop is writteni++or++i?