Alex Rivera | Logout

Increment a Integer's int value?

Asked 2010-09-28T16:57:58.557
95

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

1 Answer

111

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);
answered 2010-09-28T17:00:05.463

Your Answer