增加 Integer 的 int 值?
如何在 Java 中增加整数的值?我知道我可以使用 intValue 获取值,并且可以使用 new Integer(int i) 设置它。
playerID.intValue()++;
似乎不起作用。
注意:PlayerID 是一个使用以下命令创建的整数:
Integer playerID = new Integer(1);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
Integer
对象是不可变的,因此一旦创建它们就无法修改其值。您需要创建一个新的Integer
并替换现有的。Integer
objects are immutable, so you cannot modify the value once they have been created. You will need to create a newInteger
and replace the existing one.正如 Grodriguez 所说,
Integer
对象是不可变的。这里的问题是您试图增加玩家 ID 的int
值而不是 ID 本身。在 Java 5+ 中,您可以只编写playerID++
。附带说明一下,永远不要调用
Integer
的构造函数。通过直接将int
分配给Integer
来利用自动装箱,例如Integer foo = 5
。这将透明地使用 Integer.valueOf(int) ,这优于构造函数,因为它并不总是需要创建新对象。As Grodriguez says,
Integer
objects are immutable. The problem here is that you're trying to increment theint
value of the player ID rather than the ID itself. In Java 5+, you can just writeplayerID++
.As a side note, never ever call
Integer
's constructor. Take advantage of autoboxing by just assigningint
s toInteger
s directly, likeInteger foo = 5
. This will useInteger.valueOf(int)
transparently, which is superior to the constructor because it doesn't always have to create a new object.AtomicInteger
也许这也有一些价值:有一个名为
AtomicInteger
。这个类有一些有用的方法,例如
addAndGet(int delta)
或incrementAndGet()
(及其对应项)它允许您增加/减少同一实例的值。尽管该类设计为在并发上下文中使用,但它也非常有用在其他场景中,可能适合您的需要。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)
orincrementAndGet()
(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.Java 7 和 8。增量确实会更改引用,因此它引用另一个 Integer 对象。看:
整数本身仍然是不可变的。
Java 7 and 8. Increment DOES change the reference, so it references to another Integer object. Look:
Integer by itself is still immutable.
整数对象是不可变的。您无法更改对象本身保存的整数值,但可以创建一个新的 Integer 对象来保存结果:
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:
对于 Java 7,增量运算符“++”适用于整数。下面是一个测试过的例子
For Java 7, increment operator '++' works on Integers. Below is a tested example
也许你可以尝试:
Maybe you can try:
您可以使用 IntHolder 作为 Integer 的可变替代品。但这值得吗?
You can use IntHolder as mutable alternative to Integer. But does it worth?
所有原始包装对象都是不可变的。
我可能迟到了,但我想补充并澄清,当您执行
playerID++
时,真正发生的事情是这样的:Integer.valueOf(int) 将始终缓存范围 -128 到 127(含)内的值,并且可能缓存此范围之外的其他值。
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:Integer.valueOf(int) will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.
Java 8之后,我们可以使用如下
After Java 8, we can use as below