Is there a difference between x++ and ++x in java?

erickreutz picture erickreutz · Jul 7, 2009 · Viewed 223.1k times · Source

Is there a difference between ++x and x++ in java?

Answer

Emil H picture Emil H · Jul 7, 2009

++x is called preincrement while x++ is called postincrement.

int x = 5, y = 5;

System.out.println(++x); // outputs 6
System.out.println(x); // outputs 6

System.out.println(y++); // outputs 5
System.out.println(y); // outputs 6