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.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes