博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java整型自动装箱的缓存机制
阅读量:5357 次
发布时间:2019-06-15

本文共 3393 字,大约阅读时间需要 11 分钟。

见代码验证

/** * 整型缓存机制 * 以下缓存只在 自动装箱(autobox)时有用.new 对象无用 */public class IntegerTypeCache {    public static void main(String[] args) {        /**         * 运行日志当大于127时,autobox的引用判断为false         */        for (int i = 0; i < 200; i++) {            /**             * Integer Cache test             *             * 缓存默认-128至127,范围可能通过-XX:AutoBoxCacheMax=size 修改             */            Integer integer1 = new Integer(i);            Integer integer2 = new Integer(i);            Integer integer3 = i;            Integer integer4 = i;            boolean result1 = integer1 == integer2;            boolean result2 = integer3 == integer4;            System.out.println("integer1 " + integer1 + " =  integer2 " + integer2 + " result is " + result1);            System.out.println("integer3 " + integer3 + " =  integer4 " + integer4 + " result is " + result2);            /**             * Long Cache test             * 缓存范围为 -128至127             */            Long long1 = new Long(i);            Long long2 = new Long(i);            Integer long3 = i;            Integer long4 = i;            boolean result3 = long1 == long2;            boolean result4 = long3 == long4;            System.out.println("long1 " + long1 + " =  long2 " + long2 + " result is " + result3);            System.out.println("long3 " + long3 + " =  long4 " + long4 + " result is " + result4);            /**             *Short Cache test             *  缓存范围为 -128至127             */            Short short1 = new Short((short) i);            Short short2 = new Short((short) i);            Short short3 = (short) i;            Short short4 = (short) i;            boolean result5 = short1 == short2;            boolean result6 = short3 == short4;            System.out.println("short1 " + short1 + " =  short2 " + short2 + " result is " + result5);            System.out.println("short3 " + short3 + " =  short4 " + short4 + " result is " + result6);            System.out.println();        }        /**         * Character 缓存大小为 unicode 0-127         */        Character character1 = new Character('1'); //unicode 49        Character character2 = '1';        Character character3 = '1';        System.out.println("character1 ==  character2 " + (character1 == character2));//false        System.out.println("character3 ==  character4 " + (character2 == character3));//true        Character character4 = new Character('大');//unicode>127        Character character7 = new Character('大');        Character character5 = '大';        Character character6 = '大';        System.out.println("character4 ==  character5 " + (character4 == character5));//false        System.out.println("character5 ==  character6 " + (character5 == character6));//false        System.out.println("character4 ==  character7 " + (character4 == character7));//false        /**         * Byte 缓存大小为 -128至127         * autobox的引用比较全部为true         */        for (byte i = Byte.MIN_VALUE; i < Byte.MAX_VALUE; i++) {            Byte byte1 = new Byte(i);            Byte byte2 = new Byte(i);            Byte byte3 = i;            Byte byte4 = i;            System.out.println("byte1 " + byte1 + " == byte2 " + byte2 + " is " + (byte1 == byte2));//false            System.out.println("byte3 " + byte3 + " == byte4 " + byte4 + " is " + (byte3 == byte4));//true            System.out.println();        }    }}

 

转载于:https://www.cnblogs.com/feichen-2018/p/8991684.html

你可能感兴趣的文章
ThreadLocal实现线程范围内共享
查看>>
多校HDU5723 最小生成树+dfs回溯
查看>>
ASP.NET MVC分页实现之改进版-增加同一个视图可设置多个分页
查看>>
关于ASP.NET MVC开发设计中出现的问题与解决方案汇总 【持续更新】
查看>>
关于Entity Framework中的Attached报错的完美解决方案终极版
查看>>
Selenium之Web页面滚动条滚操作
查看>>
组合数据类型练习,英文词频统计实例上
查看>>
Uber回馈开源的一些软件
查看>>
day 3 修改haproxy.cfg 作业
查看>>
UIScrollView —— 缩放实现案例(二)
查看>>
【Qt】Qt Linguist介绍【转】
查看>>
sim usim Uim 区别
查看>>
网页中插入透明Flash的方法和技巧
查看>>
动态内存申请函数选择(realloc、malloc 、alloca、 calloc)
查看>>
获取元素属性get_attribute
查看>>
视觉设计师的进化
查看>>
Python/jquery
查看>>
【BZOJ】【2132】圈地计划
查看>>
Lua 语言基本语法
查看>>
ARM 的Thumb状态测试
查看>>