前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java中整数常量池(-128~127)上限如何调整?

java中整数常量池(-128~127)上限如何调整?

作者头像
菩提树下的杨过
发布2021-06-01 22:42:52
1.1K0
发布2021-06-01 22:42:52
举报

众所周知:java中Integer有一个常量池范围-128~127

代码语言:javascript
复制
Integer a = 500, b = 500;
System.out.println(a == b);//false
a = 100;
b = 100;
System.out.println(a == b);//true

相信也有一部分人在面试时,也被问到过这个问题,但是如果面试官继续追问:如果我想让 

代码语言:javascript
复制
Integer a = 500, b = 500;
System.out.println(a == b);

也返回true,该怎么做?没准备的同学,可能会一时被问住,其实答案就在java.lang.Integer.IntegerCache 这个类的源码上

代码语言:javascript
复制
    /**
     * Cache to support the object identity semantics of autoboxing for values between
     * -128 and 127 (inclusive) as required by JLS.
     *
     * The cache is initialized on first usage.  The size of the cache
     * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
     * During VM initialization, java.lang.Integer.IntegerCache.high property
     * may be set and saved in the private system properties in the
     * sun.misc.VM class.
     */

    private static class IntegerCache {
       // ...
    }

这个类的注释上,已经明细说明:

代码语言:javascript
复制
-XX:AutoBoxCacheMax=<size>
-Djava.lang.Integer.IntegerCache.high=<size>

这2个参数都可以控制上限。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-05-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档