Search code examples
javaclassintegerint

Increment a Integer's int value?


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);

Solution

  • 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);