Java 增加整数的 int 值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3815173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Increment a Integer's int value?
提问by William
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).
如何在 Java 中增加整数的值?我知道我可以用 intValue 获取值,我可以用 new Integer(int i) 设置它。
playerID.intValue()++;
does not seem to work.
似乎不起作用。
Note: PlayerID is a Integer that has been created with:
注意:PlayerID 是一个整数,它是用以下方法创建的:
Integer playerID = new Integer(1);
采纳答案by Grodriguez
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.
Integer
对象是不可变的,因此一旦创建它们就不能修改其值。您将需要创建一个新的Integer
并替换现有的。
playerID = new Integer(playerID.intValue() + 1);
回答by RHSeeger
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 对象来保存结果:
Integer start = new Integer(5);
Integer end = start + 5; // end == 10;
回答by Stan Kurilin
You can use IntHolder as mutable alternative to Integer. But does it worth?
您可以使用 IntHolder 作为 Integer 的可变替代品。但值得吗?
回答by ColinD
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++
.
正如 Grodriguez 所说,Integer
对象是不可变的。这里的问题是您试图增加int
玩家 ID的值而不是 ID 本身。在 Java 5+ 中,您只需编写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.
作为旁注,永远不要调用Integer
的构造函数。通过直接将int
s分配给s 来利用自动装箱Integer
,例如Integer foo = 5
. 这将使用Integer.valueOf(int)
透明,这优于构造函数,因为它并不总是必须创建一个新对象。
回答by samsamara
For Java 7, increment operator '++' works on Integers. Below is a tested example
对于 Java 7,增量运算符“++”适用于整数。下面是一个经过测试的例子
Integer i = new Integer( 12 );
System.out.println(i); //12
i = i++;
System.out.println(i); //13
回答by Vivit
AtomicInteger
AtomicInteger
Maybe this is of some worth also: there is a Java class called AtomicInteger
.
也许这也有一些价值:有一个名为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.
此类有一些有用的方法,例如addAndGet(int delta)
or incrementAndGet()
(及其对应方法),它们允许您增加/减少同一实例的值。尽管该类旨在用于concurrency的上下文中,但它在其他场景中也非常有用,并且可能适合您的需要。
final AtomicInteger count = new AtomicInteger( 0 ) ;
…
count.incrementAndGet(); // Ignoring the return value.
回答by fantouch
Maybe you can try:
也许你可以试试:
final AtomicInteger i = new AtomicInteger(0);
i.set(1);
i.get();
回答by vitvlkv
Java 7 and 8. Increment DOES change the reference, so it references to another Integer object. Look:
Java 7 和 8. Increment 确实会更改引用,因此它引用了另一个 Integer 对象。看:
@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.
Integer 本身仍然是不可变的。
回答by Hamza Belmellouki
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++
,真正发生的事情是这样的:
playerID = Integer.valueOf( playerID.intValue() + 1);
Integer.valueOf(int)will always cache values in the range -128 to 127, inclusive, and maycache other values outside of this range.
Integer.valueOf(int)将始终缓存 -128 到 127(含)范围内的值,并且可能缓存此范围之外的其他值。