Increment a Integer's int value?

前端 未结 9 2103
你的背包
你的背包 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:53

    Java 7 and 8. Increment DOES change the reference, so it references to another Integer object. Look:

    @Test
    public void incInteger()
    {
        Integer i = 5;
        Integer iOrig = i;
        ++i; // Same as i = i + 1;
        Assert.assertEquals(6, i.intValue());
        Assert.assertNotEquals(iOrig, i);
    }
    

    Integer by itself is still immutable.

提交回复
热议问题