Increment a Integer's int value?
Solution 1
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);
Solution 2
As Grodriguez says, Integer
objects are immutable. The problem here is that you're trying to increment the int
value of the player ID rather than the ID itself. In Java 5+, you can just write playerID++
.
As a side note, never ever call Integer
's constructor. Take advantage of autoboxing by just assigning int
s to Integer
s directly, like Integer foo = 5
. This will use Integer.valueOf(int)
transparently, which is superior to the constructor because it doesn't always have to create a new object.
Solution 3
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.
Solution 4
AtomicInteger
Maybe this is of some worth also: there is a Java class called AtomicInteger
.
This class has some useful methods like addAndGet(int delta)
or incrementAndGet()
(and their counterparts) which allow you to increment/decrement the value of the same instance. Though the class is designed to be used in the context of concurrency, it's also quite useful in other scenarios and probably fits your need.
final AtomicInteger count = new AtomicInteger( 0 ) ;
…
count.incrementAndGet(); // Ignoring the return value.
Solution 5
Integer objects are immutable. You can't change the value of the integer held by the object itself, but you can just create a new Integer object to hold the result:
Integer start = new Integer(5);
Integer end = start + 5; // end == 10;
Related videos on Youtube
William
Updated on May 02, 2020Comments
-
William about 4 years
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);
-
amara over 13 yearsWhy are you using Integers instead of ints?
-
Steve Blackwell over 8 years@naiad For me it's usually because you can't use primitive types as generic arguments in Java.
-
-
Joel over 13 yearsIf you must use a non-primitive int, and you want mutability, you can try commons MutableInt commons.apache.org/lang/api-2.4/org/apache/commons/lang/mutable/…
-
William over 13 yearsthanks, but for some reason it's still not incrementing. Maybe it's a bug in my code...
-
Stan Kurilin over 13 years@William : As i know, it will be incremented only in method that increment it.
-
ColinD over 13 yearsDon't use Integer's constructor.
-
Grodriguez over 13 years@ColinD: I believe that spelling it out like this makes it easier to see what is actually going on.
-
ColinD over 13 years@Grodriguez: Perhaps, though even then I'd suggest
Integer.valueOf(int)
... I don't like the idea of using bad practices in answers at all, it can lead to people thinking they're ok to do. I also think it's useful for the OP to realize that he can use operations he'd use with anint
with anInteger
in the same manner. -
Grodriguez over 13 years@ColinD: "can lead to people thinking they're ok to do so"? I'm sorry, but it is OK to use the constructor, even though
Integer.valueOf(int)
is better. The fact that there are better alternatives (where available -- please note that some people is still stuck on pre-1.5 environments for different reasons) does not mean that using the constructor is "bad practice". And I still believe that my answer was clearer the way I wrote it. -
vikingsteve over 8 yearsBut be aware that other references to
i = new Integer(12)
will still refer to12
, not13
... easy to get tripped up on this one -
samsamara over 8 yearsyeah thats why you have to reassign the value to the integer object: i = i++
-
Simon Forsberg about 7 years
i = i++
is not the same asi++
. Technically, the++
doesn't really work on integers I'd say as you can't use it by itself without assigning the result to something. -
clemep over 6 yearsyou don't have to reassign! look: Integer foo = 5; Integer bar = foo; foo++; System.out.println("foo: " + foo + " bar: " + bar); outputs: foo: 6 bar: 5
-
Julien Kronegg about 6 yearsThis is IMHO the best answer as it demonstrates the
++i
function (on variablei
; note that you could have wrotei++
as well) and theInteger
class immutability (on variableiOrig
). Most other answers demonstrate only one of the two notions. -
gary over 4 yearsplayerID = Integer.valueOf(playerID.intValue() + 1); A new Integer object is not always created. The autoboxing spec: docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.7 If the value p being boxed is true, false, a byte, or a char in the range \u0000 to \u007f, or an int or short number between -128 and 127 (inclusive), then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2. Since Java 7, docs.oracle.com/javase/specs/jls/se7/html/… also applies to the ++ operator
-
Hamza Belmellouki over 4 yearsGot it, answer edited. Incrementing the Integer object
playerID++
results in something likeplayerID = Integer.valueOf( playerID.intValue() + 1)
and according to the method's docs: This method will always cache values in the range -128 to 127, inclusive. -
Tejesh Raut over 2 yearsThat's what I was looking for! This is very useful for calling integer by reference.