Increment a Integer's int value?

前端 未结 9 2104
你的背包
你的背包 2020-12-14 05:11

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


        
9条回答
  •  孤街浪徒
    2020-12-14 05:51

    All the primitive wrapper objects are immutable.

    I'm maybe late to the question but I want to add and clarify that when you do playerID++, what really happens is something like this:

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

    Integer.valueOf(int) will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.

提交回复
热议问题