zoukankan      html  css  js  c++  java
  • java中整数常量池(-128~127)上限如何调整?

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

    Integer a = 500, b = 500;
    System.out.println(a == b);//false
    a = 100;
    b = 100;
    System.out.println(a == b);//true
    

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

    Integer a = 500, b = 500;
    System.out.println(a == b);
    

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

        /**
         * 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 {
           // ...
        }
    

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

    -XX:AutoBoxCacheMax=<size>
    -Djava.lang.Integer.IntegerCache.high=<size>
    

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

    作者:菩提树下的杨过
    出处:http://yjmyzz.cnblogs.com
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    阿里云下Linux MySQL的安装
    protocol buffer相关
    Unity NGUI UIPanel下对粒子的剪裁
    NGUI中UILabel用省略号替换超出显示区域的内容
    Go交叉编译
    Unity3d使用未破解的TexturePacker
    编程之美 找出符合条件的整数
    算法导论~
    hadoop资料汇总(网上)
    SOE 中调用第三方dll
  • 原文地址:https://www.cnblogs.com/yjmyzz/p/14829482.html
Copyright © 2011-2022 走看看