0
public static void main(String[] args) {
    int x = 10;
    x = x++;
    x = x++;
    x = x++;
    System.out.println(x);
}

Why is the output 10 when the expected output is 13?

Abhishek
  • 2,485
  • 2
  • 18
  • 25

2 Answers2

3

Post increment operator x++ returns the original value of x. Therefore x=x++ assigns the old value of x back to x.

Eran
  • 387,369
  • 54
  • 702
  • 768
0

This is probably what you weant to do

public static void main(String[] args) {
    int x = 10;
    x++;
    x++;
    x++;
    System.out.println(x);
}
Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47