为什么整数 ==
运算符不适用于 128 及之后的整数值?有人能解释一下这种情况吗?
Why Integer ==
operator does not work for 128 and after Integer values? Can someone explain this situation?
这是我的 Java 环境:
This is my Java environment:
java version "1.6.0_37"
Java(TM) SE Runtime Environment (build 1.6.0_37-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.12-b01, mixed mode)
示例代码:
Integer a;
Integer b;
a = 129;
b = 129;
for (int i = 0; i < 200; i++) {
a = i;
b = i;
if (a != b) {
System.out.println("Value:" + i + " - Different values");
} else {
System.out.println("Value:" + i + " - Same values");
}
}
部分控制台输出:
Value:124 - Same values
Value:125 - Same values
Value:126 - Same values
Value:127 - Same values
Value:128 - Different values
Value:129 - Different values
Value:130 - Different values
Value:131 - Different values
Value:132 - Different values
查看Integer 的源代码 .您可以在那里看到值的缓存.
Check out the source code of Integer . You can see the caching of values there.
缓存仅在您使用 Integer.valueOf(int)
时发生,而不是在您使用 new Integer(int)
时发生.您使用的自动装箱使用 Integer.valueOf
.
The caching happens only if you use Integer.valueOf(int)
, not if you use new Integer(int)
. The autoboxing used by you uses Integer.valueOf
.
根据 JLS,您始终可以相信,对于介于 -128 和 127 之间的值,在自动装箱后您会获得相同的 Integer 对象,并且在某些实现中,即使对于更高的值,您也可能会获得相同的对象.
According to the JLS, you can always count on the fact that for values between -128 and 127, you get the identical Integer objects after autoboxing, and on some implementations you might get identical objects even for higher values.
实际上在 Java 7 中(我认为在 Java 6 的较新版本中),IntegerCache 类的实现 已更改,上限不再是硬编码,但可以通过属性java.lang.Integer.IntegerCache.high"进行配置,所以如果你使用 VM 参数 -Djava.lang.Integer.IntegerCache.high=1000
运行您的程序,您将获得相同的值";所有值.
Actually in Java 7 (and I think in newer versions of Java 6), the implementation of the IntegerCache class has changed, and the upper bound is no longer hardcoded, but it is configurable via the property "java.lang.Integer.IntegerCache.high", so if you run your program with the VM parameter -Djava.lang.Integer.IntegerCache.high=1000
, you get "Same values" for all values.
但是 JLS 仍然保证它只到 127:
But the JLS still guarantees it only until 127:
理想情况下,装箱一个给定的原始值 p,总是会产生一个相同的引用.在实践中,使用现有的实现技术这可能是不可行的.上述规则是一种务实的妥协.上面的最后一个条款要求某些常见的值总是被装箱到无法区分的对象中.实现可能会延迟或急切地缓存这些内容.
Ideally, boxing a given primitive value p, would always yield an identical reference. In practice, this may not be feasible using existing implementation techniques. The rules above are a pragmatic compromise. The final clause above requires that certain common values always be boxed into indistinguishable objects. The implementation may cache these, lazily or eagerly.
对于其他值,此公式不允许程序员对装箱值的身份进行任何假设.这将允许(但不要求)共享部分或全部这些引用.
For other values, this formulation disallows any assumptions about the identity of the boxed values on the programmer's part. This would allow (but not require) sharing of some or all of these references.
这可确保在最常见的情况下,行为将是所需的行为,而不会造成过度的性能损失,尤其是在小型设备上.例如,内存限制较少的实现可能会缓存所有字符和短整数,以及 -32K - +32K 范围内的整数和长整数.
This ensures that in most common cases, the behavior will be the desired one, without imposing an undue performance penalty, especially on small devices. Less memory-limited implementations might, for example, cache all characters and shorts, as well as integers and longs in the range of -32K - +32K.
这篇关于为什么相等运算符适用于整数值直到 128 数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!