Java自动装箱/拆箱机制本质
2020/3/23 17:31:20
本文主要是介绍Java自动装箱/拆箱机制本质,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
前言
近期遇到一个于Java自动装箱/拆箱有关的问题,如下:
public class Test03 { public static void main(String[] args) { Integer f1 = 100, f2 = 100, f3 = 150, f4 = 150; System.out.println(f1 == f2); System.out.println(f3 == f4); // 输出是啥?? } }复制代码
SO EASY!? 答案 false, false 理由:Integer自动装箱,相当于 Integer f1 = new Integer(100), f2 = new Integer(100),f3......
在堆内存里占用不同的对象空间,== 比较引用,所以false, false
然而结果是:
不急,下面来揭晓 (●'◡'●)
装箱来源
Java 是一个近乎纯洁的面向对象编程语言,但是为了编程的方便还是引入了基本
数据类型,但是为了能够将这些基本数据类型当成对象操作,Java 为每一个基本数据类型都引入了对应的包装类型(wrapper class),int 的包装类就是 Integer,
从 Java 5 开始引入了自动装箱/拆箱机制,使得二者可以相互转换。
Java 为每个原始类型提供了包装类型:
- 原始类型: boolean,char,byte,short,int,long,float,double
- 包装类型:Boolean,Character,Byte,Short,Integer,Long,Float, Double
装箱本质
当我们给一个 Integer 对象赋一个 int 值的时候,(如:Integer i = 198
)会调用 Integer 类的静态方法 valueOf
,如果看看 valueOf
的源代码就知道发生了什么。
valueOf分析
public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }复制代码
这里 i 就是198,首先与 IntegerCache类(Integer内部静态类)里的low,high这两个属性比较,在这范围内就从cache里取出来,没有在范围内就直接返回一个新的在堆内存分配的Integer(198),那这个IntegerCache就是关键,因为没有if()...... 这段,那就是返回new Integer,== 比较肯定为false,下面来看看
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=} 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 { static final int low = -128; static final int high; static final Integer cache[]; static { // 先设定一个high值 int h = 127; // 下面 integerCacheHighPropValue是通过 JVM设置-XX:AutoBoxCacheMax= 获得的 String integerCacheHighPropValue = sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); if (integerCacheHighPropValue != null) { try { int i = parseInt(integerCacheHighPropValue); i = Math.max(i, 127); // Maximum array size is Integer.MAX_VALUE h = Math.min(i, Integer.MAX_VALUE - (-low) -1); } catch( NumberFormatException nfe) { // If the property cannot be parsed into an int, ignore it. } } // 这步给 high属性赋值,即使high被static final修饰 high = h; // 给 cache 赋一个数组,并且从 low(-128)开始+1递增赋给数组,直到high(这里不设置VM的话就是127) cache = new Integer[(high - low) + 1]; int j = low; for(int k = 0; k < cache.length; k++) cache[k] = new Integer(j++); // range [-128, 127] must be interned (JLS7 5.1.7) assert IntegerCache.high >= 127; } private IntegerCache() {} } 复制代码
IntegerCache是Integer内部静态类,当JVM启动后,会自动加载java.lang下面的类,Integer类会被加载到方法区,之后是验证阶段,准备阶段时,会给static方法,static类(IntegerCache),static属性 分配空间,但不会赋值。然后常量池内加载IntegerCache类的 static final int low,static final int high, static final Integer cache[]
初始化阶段,给low赋值 -128,并且执行static代码块,static代码块里面会给high属性赋值,即使high被final修饰
看完IntegerCache后,回到ValueOf片段,从cache(位于常量池,-128 ~ 127)取值,应该就很清楚了 。简单的说,如果整型字面量的值在-128 到 127 之间,那么不会 new 新的 Integer 对象,而是直接引用常量池中的 Integer 对象 ( ̄︶ ̄)↗
if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)];复制代码
总结
回到原先的问题
public class Test03 { public static void main(String[] args) { Integer f1 = 100, f2 = 100, f3 = 150, f4 = 150; System.out.println(f1 == f2); System.out.println(f3 == f4); // 输出是啥?? } }复制代码
f1, f2装箱,100位于 -128 ~ 127之间,直接指向常量池的cache[100 + (--128)],所以 == 比较为true,f3, f4不在,所以直接在堆内存中分配Integer对象,== 为false
补充
再补充一个问题 (~ ̄▽ ̄)~
class AutoUnboxingTest { public static void main(String[] args) { Integer a = new Integer(3); Integer b = 3; // 将 3 自动装箱成 Integer 类型 int c = 3; System.out.println(a == b); // false 两个引用没有引用同一对象 System.out.println(a == c); // true a 自动拆箱成 int 类型再和c比较 } }复制代码
遇到Integer b = 3 这样的就要自动装箱,a指向堆内存某块,b指向常量池某块,==为false
注意: Integer类型与int基本类型比较,要拆箱,Integer转为int,值为3,== 是值比较,所以为true
希望对你有帮助 (o゚v゚)ノ
这篇关于Java自动装箱/拆箱机制本质的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南