Skip to content

Demonstrate pre/ post increment operators better #149

@Locust1

Description

@Locust1

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

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);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions