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,427
  • 2
  • 17
  • 24

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
  • 374,785
  • 51
  • 663
  • 734
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,127
  • 5
  • 31
  • 44