zoukankan      html  css  js  c++  java
  • 关于Java自动拆箱装箱中的缓存问题

     1 package cn.zhang.test;
     2 /**
     3  * 测试自动装箱拆箱
     4  * 自动装箱:基本类型自动转为包装类对象
     5  * 自动拆箱:包装类对象自动转化为基本数据类型
     6  * 
     7  * 
     8  * /*缓存问题*/
     9         /*缓存[-128,127]之间的数字,也就是一个byte,实际上是系统在初始的时候创建了一个范围在[-128,127]之间的一个数组
    10          * 当我们调用valueOf的时候,首先判断该数字是否在[-128,127]之间,如果在,则在数组中拿出该对象,侧面印证了数组
    11          * 本身就是对象,如果不在,则创建一个新的Integer对象
    12  * 
    13  * @author 张涛
    14  *
    15  */
    16 public class TestAutoBox {
    17     public static void main(String[] args) {
    18         Integer a = 10; //自动装箱 编译器会自动改成:Integer a = Integer.valueOf(10);
    19         
    20         int b = a; //自动拆箱 编译器会自动改成:int b = a.intValue();
    21         
    22 //        int c = a.intValue();
    23 //        System.out.println(b);
    24 //        System.out.println(c);
    25         
    26 //        Integer c = null;
    27 //        int d = c;    //空指针错误,因为调用了c.intValue();这个方法
    28         
    29         /*缓存问题*/
    30         /*缓存[-128,127]之间的数字,也就是一个byte,实际上是系统在初始的时候创建了一个范围在[-128,127]之间的一个数组
    31          * 当我们调用valueOf的时候,首先判断该数字是否在[-128,127]之间,如果在,则在数组中拿出该对象,侧面印证了数组
    32          * 本身就是对象,如果不在,则创建一个新的Integer对象
    33          * */
    34         Integer e = -128;
    35         Integer f = Integer.valueOf(-128);
    36         System.out.println(e == f);//true
    37         System.out.println(e.equals(f));//true
    38         
    39         Integer g = 1234;
    40         Integer h = Integer.valueOf(1234);
    41         System.out.println(g == h);//false
    42         System.out.println(g.equals(h));//true
    43     }
    44 }
  • 相关阅读:
    一个微信小程序跳转到另一个微信小程序
    微信小程序 canIUse
    微信小程序 点击事件 传递参数
    jquery 在将对象作为参数传递的时候要转换成 JSON
    微擎后台进行GET提交
    微信小程序添加底部导航栏
    jquery 中 html与text函数的区别
    C++ int与char[]的相互转换
    Qt error: LNK1158 无法运行rc.exe解决办法
    Qt error C3615: constexpr 函数 "qCountLeadingZeroBits" 不会生成常数表达式
  • 原文地址:https://www.cnblogs.com/zhangqiling/p/11378791.html
Copyright © 2011-2022 走看看