The increment of integer value int?

How to increase integer value in java? I know that I can get the value using intValue, and I can set it using the new Integer (int i).

playerID.intValue()++; 

doesn't seem to work.

Note. PlayerID is an integer that was created using

 Integer playerID = new Integer(1); 
+74
java class integer int
Sep 28 '10 at 16:57
source share
9 answers

Integer objects are immutable, so you cannot change the value after they are created. You will need to create a new Integer and replace the existing one.

 playerID = new Integer(playerID.intValue() + 1); 
+90
Sep 28 '10 at 17:00
source share

As Grodriguez says, Integer objects are immutable. The problem here is that you are trying to increase the int value of the player identifier, not the identifier itself. In Java 5+, you can simply write playerID++ .

As a side note, never call the Integer constructor. Take advantage of autoboxing by simply setting int to Integer directly, e.g. Integer foo = 5 . This will use the transparent Integer.valueOf(int) , which is superior to the constructor because it does not always have to create a new object.

+47
Sep 28 '10 at 17:06
source share

Java 7 and 8. Increment DOES change the link, so it refers to another Integer object. Take a 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); } 

The integer itself remains unchanged.

+10
Jan 29 '18 at 7:42
source share

Perhaps this is the value: there is a Java class called AtomicInteger (see https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicInteger.html ). This class has some useful methods, such as addAndGet(int delta) or incrementAndGet() (and their analogs), which allow you to increase / decrease the value of the same instance. Although the class is intended to be used in the context of concurrency, it is also very useful in other scenarios and probably suits you.

+8
Feb 18 '16 at 11:17
source share

Integer objects are immutable. You cannot change the value of the integer held by the object itself, but you can simply create a new Integer object to store the result:

 Integer start = new Integer(5); Integer end = start + 5; // end == 10; 
+7
Sep 28 '10 at 17:03
source share

For Java 7, the increment operator "++" works with integers. Below is an example of testing

  Integer i = new Integer( 12 ); System.out.println(i); //12 i = i++; System.out.println(i); //13 
+6
Mar 16 '15 at 4:02
source share

Perhaps you can try:

 final AtomicInteger i = new AtomicInteger(0); i.set(1); i.get(); 
+4
Jul 11 '17 at 2:33 on
source share

You can use IntHolder as a mutable alternative to Integer. But is it worth it?

+3
Sep 28 '10 at 17:05
source share

All primitive wrapper objects are immutable.

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

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

As you can see, a new Integer object has been created.

0
Aug 19 '19 at 20:52 on
source share



All Articles