为什么等于运算符的Integer值直到128数? [英] Why equal operator works for Integer value until 128 number?

查看:133
本文介绍了为什么等于运算符的Integer值直到128数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么Integer =运算符不能用于128和Integer值之后?有人可以解释这种情况吗?

Why Integer "=" operator does not work for 128 and after Integer values? Can someone explain this situation?

这是我的Java环境:
java版1.6.0_37

This is my Java environment: java version "1.6.0_37"

Java(TM)SE运行时环境(版本1.6.0_37-b06)

Java(TM) SE Runtime Environment (build 1.6.0_37-b06)

Java HotSpot(TM)64位服务器VM (建立20.12-b01,混合模式)

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类的实现已经改变,而上层bound不再是硬编码的,但可以通过属性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:


理想情况下,拳击给予n原始值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.

这篇关于为什么等于运算符的Integer值直到128数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆