为什么整数常量池的行为在 127 处发生变化? [英] Why does the behavior of the Integer constant pool change at 127?

查看:24
本文介绍了为什么整数常量池的行为在 127 处发生变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解 Java 整数常量池的工作原理.

I am not able to understand how the Java Constant Pool for Integer works.

我了解字符串的行为,因此能够证明自己对整数常量也是如此.

I understand the behavior of Strings, and hence able to justify myself that it is the same case with Integer Constants also.

所以,对于整数

Integer i1 = 127;
Integer i2 = 127;
System.out.println(i1==i2); // True

&

Integer i1 = new Integer(127);
Integer i2 = new Integer(127);
System.out.println(i1==i2); // False

直到这里一切都在我的脑海中.

Till here everything goes in my head.

我无法理解的是,当我从 127 增加整数时,它的行为有所不同.这种行为在 127 之后发生了变化,下面是代码片段

What I am not able to digest is, it behaves differently when I increase the integer from 127. This behavior changes after 127, below is the code snippet

Integer i1 = 128;
Integer i2 = 128;
System.out.println(i1==i2); // False. WHY?????

有人能帮我理解这个吗?

Can somebody help me understand this?

推荐答案

不,数字常量池的工作方式与字符串不同.对于字符串,只有编译时常量被插入 - 而对于整数类型的包装类型,任何装箱操作将总是使用池,如果它适用于该值.例如:

No, the constant pool for numbers doesn't work the same way as for strings. For strings, only compile-time constants are interned - whereas for the wrapper types for integer types, any boxing operation will always use the pool if it's applicable for that value. So for example:

int x = 10;
int y = x + 1;
Integer z = y; // Not a compile-time constant!
Integer constant = 11;
System.out.println(z == constant); // true; reference comparison

JLS 保证池化值的范围很小,但如果他们愿意,实现可以使用更广泛的范围.

The JLS guarantees a small range of pooled values, but implementations can use a wider range if they wish.

请注意,虽然不能保证,但我看过的每个实现都使用 Integer.valueOf 来执行装箱操作 - 这样你就可以在没有语言帮助的情况下获得相同的效果:

Note that although it's not guaranteed, every implementation I've looked at uses Integer.valueOf to perform boxing operations - so you can get the same effect without the language's help:

Integer x = Integer.valueOf(100);
Integer y = Integer.valueOf(100);
System.out.println(x == y); // true

来自 第 5.1 节.JLS 中的 7 个:

如果被装箱的值 p 是真、假、字节或 u0000 到 u007f 范围内的字符,或者是 -128 到 127(含)之间的整数或短数,则令 r1 和 r2 为p 的任意两次装箱转换的结果.r1 == r2 总是如此.

If the value p being boxed is true, false, a byte, or a char in the range u0000 to u007f, or an int or short number between -128 and 127 (inclusive), then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.

理想情况下,装箱给定的原始值 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.

这可确保在大多数常见情况下,行为将是所需的行为,而不会造成过度的性能损失,尤其是在小型设备上.例如,内存限制较少的实现可能会缓存所有 char 和 short 值,以及 -32K 到 +32K 范围内的 int 和 long 值.

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 char and short values, as well as int and long values in the range of -32K to +32K.

这篇关于为什么整数常量池的行为在 127 处发生变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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