Increment a Integer's int value?

William picture William · Sep 28, 2010 · Viewed 208.9k times · Source

How do I increment a Integer's value in Java? I know I can get the value with intValue, and I can set it with new Integer(int i).

playerID.intValue()++;

does not seem to work.

Note: PlayerID is a Integer that has been created with:

Integer playerID = new Integer(1);

Answer

Grodriguez picture Grodriguez · Sep 28, 2010

Integer objects are immutable, so you cannot modify the value once they have been created. You will need to create a new Integer and replace the existing one.

playerID = new Integer(playerID.intValue() + 1);