这篇文章主要介绍“Java中的装箱与拆箱是什么意思”,在日常操作中,相信很多人在Java中的装箱与拆箱是什么意思问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Java中的装箱与拆箱是什么意思”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
一、Java数据类型
1、在说装箱与拆箱之前,先说一下Java的基本数据类型,Java从数据类型上可以划分为值类型与引用类型,值类型是四类八种,分别是:
| 数据类型 | 内存 | 默认值 | 包装类 |
|---|
| byte | 8位 | 0 | Byte |
| short | 16位 | 0 | short |
| int | 32位 | 0 | Integer |
| long | 64位 | 0L或0l | Long |
| float | 32位 | 0.0F或0.0f | Float |
| double | 64位 | 0.0D或0.0d | Double |
| char | 16位 | \u0000 | Character |
| boolean | 8位 | flase | Boolean |
2、引用类型:
数组
类(class)
接口(Interface)
枚举(enum)

3、值类型与引用类型的区别
1. 从概念方面上来说:
值类型:变量名指向具体的值
引用类型:变量名指向数据对象的内存地址
2. 从内存构建方面上来说:
3. 从使用方面上来说:
二、Java数据类型转换
1、自动转换
运算中,不同类型的数据先转化为同一类型,然后进行运算
| 操作数1类型 | 操作数2类型 | 转换后的类型 |
|---|
| byte、short、char | int | int |
| byte、short、char、int | long | long |
| byte、short、char、int、long | float | float |
| byte、short、char、int、long、float | double | double |
2、强制转换
int x; double y; x = (int)3.14 + (int)5.20 //精度丢失 y = (double)x + (double)8 //精度提升 输出:x = 8;y = 16.0
三、Java之装箱与拆箱
1、包装类
2、什么是装箱与拆箱
装箱:将值类型装换成引用类型的过程
拆箱:将引用类型转换成值类型的过程
自动装箱:
int x = 3; Integer y = x; //int --> Integer,Integer y = x <==> Integer y = Integer.valueOf(x)
Integer x = new Integer(5); int y = x; //Integer --> int,int y = x <==> int y = x.intValue()
3、装箱和拆箱是如何实现的
4、注意点:
鸿蒙官方战略合作共建——HarmonyOS技术社区
大量使用自动拆装箱会使性能降低,还会造成大量的内存消耗
在重载方法中,可能出现问题
List<Integer> list = new ArrayList<>(); Integer x,y,z; x = 1;y = 2;z = 4; list.add(x);list.add(y);list.add(z); list.remove(2);

在上面这段代码中ArrayList.remove方法有两个重载方法,那么list.remove(2)是调用了哪个方法,remove掉的是值为2的对象,还是remove了index为2,值为4的那个对象呢?
在这种情况下,编译器不会进行自动拆装箱,所以调用的是remove(int index),index为2值为4的这个Integer对象会被remove.
如果要调用 remove(Object o)的方法,应该这么写 list.remove(y)
3. 缓存值问题
Integer i1 = 100; Integer i2 = 100; Integer i3 = 200; Integer i4 = 200; System.out.println(i1==i2); System.out.println(i3==i4); Output: true false
Intteger.valueOf方法
public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }IntegerCache类
private static class IntegerCache { static final int low = -128; static final int high; static final Integer cache[]; static { // high value may be configured by property int h = 127; 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. } } hhigh = h; 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() {} }从源码可以看出,在通过valueOf方法创建Integer对象的时候,如果数值在[-128,127]之间,便返回指向IntegerCache.cache中已经存在的对象的引用;否则创建一个新的Integer对象
Byte、Short、Integer、Long四种包装类默认创建了数值为[-128,127]的相应类型的缓存数据,但是超出此范围仍会创建新的对象。
Character默认会创建[0,127]的响应类型的缓存数据
两种浮点型没有实现常量池技术,在某个范围内的整型数值的个数是有限的,而浮点数却不是
| 包装类 | 常量池 | 常量池范围 |
|---|
| Byte | 存在 | [-128,127] |
| Short | 存在 | [-128,127] |
| Integer | 存在 | [-128,127] |
| Long | 存在 | [-128,127] |
| Character | 存在 | [0,127] |
| Float | 不存在 | 无 |
| Double | 不存在 | 无 |
到此,关于“Java中的装箱与拆箱是什么意思”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注天达云网站,小编会继续努力为大家带来更多实用的文章!