問題描述
為什么整數(shù) ==
運算符不適用于 128 及之后的整數(shù)值?有人能解釋一下這種情況嗎?
Why Integer ==
operator does not work for 128 and after Integer values? Can someone explain this situation?
這是我的 Java 環(huán)境:
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)
時發(fā)生,而不是在您使用 new Integer(int)
時發(fā)生.您使用的自動裝箱使用 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
.
根據(jù) JLS,您始終可以相信,對于介于 -128 和 127 之間的值,在自動裝箱后您會獲得相同的 Integer 對象,并且在某些實現(xiàn)中,即使對于更高的值,您也可能會獲得相同的對象.
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 類的實現(xiàn) 已更改,上限不再是硬編碼,但可以通過屬性java.lang.Integer.IntegerCache.high"進行配置,所以如果你使用 VM 參數(shù) -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,總是會產(chǎn)生一個相同的引用.在實踐中,使用現(xiàn)有的實現(xiàn)技術(shù)這可能是不可行的.上述規(guī)則是一種務(wù)實的妥協(xié).上面的最后一個條款要求某些常見的值總是被裝箱到無法區(qū)分的對象中.實現(xiàn)可能會延遲或急切地緩存這些內(nèi)容.
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.
對于其他值,此公式不允許程序員對裝箱值的身份進行任何假設(shè).這將允許(但不要求)共享部分或全部這些引用.
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.
這可確保在最常見的情況下,行為將是所需的行為,而不會造成過度的性能損失,尤其是在小型設(shè)備上.例如,內(nèi)存限制較少的實現(xiàn)可能會緩存所有字符和短整數(shù),以及 -32K - +32K 范圍內(nèi)的整數(shù)和長整數(shù).
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.
這篇關(guān)于為什么相等運算符適用于整數(shù)值直到 128 數(shù)字?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!