为什么整数类缓存在 -128 到 127 范围内的值? [英] Why Integer class caching values in the range -128 to 127?

查看:29
本文介绍了为什么整数类缓存在 -128 到 127 范围内的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于我之前的问题,为什么 == 与 Integer.valueOf(String) 的比较给出不同的127 和 128? 的结果,我们知道 Integer class 有一个缓存,用于存储 -128 之间的值127.

Regarding my previous Question, Why do == comparisons with Integer.valueOf(String) give different results for 127 and 128? , we know that Integer class has a cache which stores values between -128 and 127.

只是想知道,为什么介于 -128 和 127 之间?

Integer.valueOf()文档声明它"缓存频繁请求的值".但是 -128127 之间的值是否经常被请求?我认为经常请求的值是非常主观的.
这背后有什么可能的原因吗?

Integer.valueOf() documentation stated that it "caching frequently requested values" . But does values between -128 and 127 are frequently requested for real? I thought frequently requested values are very subjective.
Is there any possible reason behind this?

文档中还指出:..并且可能缓存此范围之外的其他值."
这是如何实现的?

From the documentation also stated: "..and may cache other values outside of this range."
How is this can be achieved?

推荐答案

只是想知道,为什么在 -128 和 127 之间?

可以缓存更大范围的整数,但至少那些介于 -128 和 127 之间的必须被缓存,因为 Java 语言规范(重点是我的):

A larger range of integers may be cached, but at least those between -128 and 127 must be cached because it is mandated by the Java Language Specification (emphasis mine):

如果被装箱的值 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.

此要求的基本原理在同一段落中进行了解释:

The rationale for this requirement is explained in the same paragraph:

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

这可确保在大多数常见情况下,行为将是理想的行为,而不会造成不当的性能损失,尤其是在小型设备上.例如,内存限制较少的实现可能会缓存所有 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.


如何缓存此范围之外的其他值.?

您可以使用 -XX:AutoBoxCacheMax JVM 选项,该选项并未真正记录在 可用的热点 JVM 选项.但是它在 590 行附近 Integer 类中的注释:

You can use the -XX:AutoBoxCacheMax JVM option, which is not really documented in the list of available Hotspot JVM Options. However it is mentioned in the comments inside the Integer class around line 590:

缓存的大小可以由 -XX:AutoBoxCacheMax= 选项控制.

The size of the cache may be controlled by the -XX:AutoBoxCacheMax=<size> option.

请注意,这是特定于实现的,可能在其他 JVM 上可用,也可能不可用.

Note that this is implementation specific and may or may not be available on other JVMs.

这篇关于为什么整数类缓存在 -128 到 127 范围内的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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