You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Both System.out.println(++i);
and System.out.println(i++);
produce the same output so it might be confusing as to what is the actual difference.
I would suggest replacing it with something along the lines of
int i = 3;
i++;
// prints "i = 4"
System.out.println("i = " + i);
++i;
// prints "i = 5"
System.out.println("i = " + i);
int j = ++i;
// prints "i = 6"
System.out.println("i = " + i);
// prints "j = 6"
System.out.println("j = " + j);
int k = i++;
// prints "i = 7"
System.out.println("i = " + i);
// prints "k = 6"
System.out.println("k = " + k);
The text was updated successfully, but these errors were encountered:
Alternatively (instead of introducing two new variables), it might be a good idea to change the comments to include how the operators change the variables.
Something along the lines of the following (I'm just showing the idea, there's probably a better text for these things):
classPrePostDemo {
publicstaticvoidmain(String[] args){
inti = 3;
i++;
// prints 4System.out.println(i);
++i;
// prints 5System.out.println(i);
// sets i to 6 and prints 6System.out.println(++i);
// sets i to 7 and prints 6System.out.println(i++);
// prints 7System.out.println(i);
}
}
The second code snippet from the Unary section does not illustrate the core difference between the pre and post increment operators well (++i vs i++):
https://dev.java/learn/language-basics/using-operators/#unary
Both
System.out.println(++i);
and
System.out.println(i++);
produce the same output so it might be confusing as to what is the actual difference.
I would suggest replacing it with something along the lines of
The text was updated successfully, but these errors were encountered: