为什么相等运算符适用于整数值直到 128 个数字? [英] Why equal operator works for Integer value until 128 number?

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

问题描述

为什么整数 == 运算符不适用于 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");
    }
}

控制台输出的某些部分:

Some part of console output:

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 个数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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