That will the following code display on the screen:

class Main {
    public static void main(String[] args) {
        int i = 0;
        System.out.println(++i == i++);
        System.out.println(i++ == i++);
    }
}
Explanation

A ++ operator increments the value of the variable by one. By its result is dependent on the locatoin of the ++ operator. Value of the i++ expression is equal to the initial value of i variable (before the increment), but the value of the ++i expression is equal already incremented value of i variable.

First expression compares two values ​​of the i variable:
- after the first increment (1) on the left
- before the second increment (1 again) on the right.
Schematically it may be depicted as follows: 0 → 1 == 1 → 2
true. is the result.

Second expression compares the following values of i varuable:
- before the third increase (2) on the left
- before the fourth increase (3) on the right
Schematically it may be depicted as follows: 2 → 3 == 3 → 4
And false. is the result this time

Java
Java Quiz
Start Quiz

or Read more about Java Quiz

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback