What will the following code print out:

public class Increments {
    public static void main(String[] args) {
        int i = 0;
        i = i++ + i++;
        System.out.println(i);
    }
}
Explanation
Java evaluates operands left-to-right.
Result of first evaluation of i++ is 0, second i++ equals 1. After the second evaluation i equals 2.
But result of final evaluation 0+1 will overwrite i and set it to 1.
Java
Java Quiz
Start Quiz

or Read more about Java Quiz

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback