Java中自动装箱与拆箱
一、什么是封装类?
Java中存在基础数据类型,但是在某些情况下,我们要对基础数据类型进行对象的操作,例如,集合中只能存在对象,而不能存在基础数据类型,于是便出现了包装器类。包装器类型就是对基本数据类型进行了封装,使之成为一个对象,每一个基本数据类型都对应一种包装器类型。

二、什么是装箱与拆箱
将基本数据类型变为包装器类,便是装箱,将包装器类转为基本数据类型就是拆箱。相面以Integer为例:
public static void main(String[] args){
//自动装箱
Integer a = 100;
//自动拆箱
int b = a;
}
实际上,Integer在装箱过程中调用了Integer中的valueOf()方法,拆箱为int时调用了Integer中的intValue()方法,源码如下:
public static Integer valueOf(int i) {
assert IntegerCache.high >= 127;
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
public int intValue() {
return value;
}
这便是装箱与拆箱的底层执行过程,其它包装器类类似。
三、自动装箱与拆箱中的一些问题
问题1:
public class Main {
public static void main(String[] args) {
Integer i1 = 100;
Integer i2 = 100;
Integer i3 = 200;
Integer i4 = 200;
System.out.println(i1==i2); //true
System.out.println(i3==i4); //false
}
}
通过分析valueOf()方法我们知道,当值为-128到127之间时,返回return IntegerCache.cache[i + (-IntegerCache.low)];当不在这个区间时,返回return new Integer(i);那么IntegerCache.cache[i + (-IntegerCache.low)]又是什么呢?继续追踪源码:
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) {
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);
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
}
private IntegerCache() {}
}
从上面代码可以看出Integer类中提前就初始化了一个Integer数组,范围为-128到127。回到上面的题目:i1==i2=100返回true是因为范围属于-128到127,直接返回cache[]数组中的对象,属于同一个对象,==运算符比较对象,从而返回true,而200超过了这个范围,直接返回new Integer(),因此属于两个不同的对象,结果为false。
问题2:
public class Main {
public static void main(String[] args) {
Double i1 = 100.0;
Double i2 = 100.0;
Double i3 = 200.0;
Double i4 = 200.0;
System.out.println(i1==i2); //false
System.out.println(i3==i4); //false
}
}
上面的代码中的结果与Integer类型的结果大相径庭,原因还得从源码中找。
public static Double valueOf(double d) {
return new Double(d);
}
因此返回false理所当然。
总结一下:
Integer派别:Integer、Short、Byte、Character、Long这几个类的valueOf方法的实现是类似的。
Double派别:Double、Float的valueOf方法的实现是类似的。每次都返回不同的对象。
Integer派别的范围:

问题3:
public class Main {
public static void main(String[] args) {
Boolean i1 = false;
Boolean i2 = false;
Boolean i3 = true;
Boolean i4 = true;
System.out.println(i1==i2);//true
System.out.println(i3==i4);//true
}
}
以上代码又是林外一种情况,继续追踪源码
public static Boolean valueOf(boolean b) {
return (b ? TRUE : FALSE);
}
/**
* The {@code Boolean} object corresponding to the primitive
* value {@code true}.
*/
public static final Boolean TRUE = new Boolean(true); /**
* The {@code Boolean} object corresponding to the primitive
* value {@code false}.
*/
public static final Boolean FALSE = new Boolean(false);
分析可知,TRUE和FALSE是两个final对象,因此i1、i2和i3、i4始终指向同一个对象的地址,因此返回true。
问题4:
public static void main(String[] args) {
Integer num1 = 400;
int num2 = 400;
System.out.println(num1 == num2); //true
System.out.println(num1.equals(num2)); //true
}
可见num1 == num2进行了拆箱操作。equals源码如下:
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
我们知道equal比较的是内容本身,并且我们也可以看到equal的参数是一个Object对象,我们传入的是一个int类型,所以首先会进行装箱,然后比较,之所以返回true,是由于它比较的是对象里面的value值。
问题5:
public static void main(String[] args) {
Integer num1 = 100;
int num2 = 100;
Long num3 = 200l;
System.out.println(num1 + num2); //200
System.out.println(num3 == (num1 + num2)); //true
System.out.println(num3.equals(num1 + num2)); //false
}
当一个基础数据类型与封装类进行==、+、-、*、/运算时,会将封装类进行拆箱,对基础数据类型进行运算。
对于num3.equals(num1 + num2)为false的原因很简单,我们还是根据代码实现来说明:
public boolean equals(Object obj) {
if (obj instanceof Long) {
return value == ((Long)obj).longValue();
}
return false;
}
它必须满足两个条件才为true:
1、类型相同
2、内容相同
上面返回false的原因就是类型不同。
问题6:
public static void main(String[] args) {
int num1 = 100;
int num2 = 200;
long mum3 = 300;
System.out.println(mum3 == (num1 + num2)); //true
}
当 “==”运算符的两个操作数都是包装器类型的引用,则是比较指向的是否是同一个对象,而如果其中有一个操作数是表达式(即包含算术运算)则比较的是数值(即会触发自动拆箱的过程)。
问题7:
public static void main(String[] args) {
Integer integer100=null;
int int100=integer100;
}
这两行代码是完全合法的,完全能够通过编译的,但是在运行时,就会抛出空指针异常。其中,integer100为Integer类型的对象,它当然可以指向null。但在第二行时,就会对integer100进行拆箱,也就是对一个null对象执行intValue()方法,当然会抛出空指针异常。所以,有拆箱操作时一定要特别注意封装类对象是否为null。
Java中自动装箱与拆箱的更多相关文章
- 深入剖析Java中的装箱和拆箱
深入剖析Java中的装箱和拆箱 自动装箱和拆箱问题是Java中一个老生常谈的问题了,今天我们就来一些看一下装箱和拆箱中的若干问题.本文先讲述装箱和拆箱最基本的东西,再来看一下面试笔试中经常遇到的与装箱 ...
- 从别人那淘的知识 深入剖析Java中的装箱和拆箱
(转载的海子的博文 海子:http://www.cnblogs.com/dolphin0520/) 深入剖析Java中的装箱和拆箱 自动装箱和拆箱问题是Java中一个老生常谈的问题了,今天我们就来 ...
- 第六节:详细讲解Java中的装箱与拆箱及其字符串
前言 大家好,给大家带来详细讲解Java中的装箱与拆箱及其字符串的概述,希望你们喜欢 装箱与拆箱 封装类有:Byte , short , Integer , Character , long , Fl ...
- [ 转载 ]学习笔记-深入剖析Java中的装箱和拆箱
深入剖析Java中的装箱和拆箱 自动装箱和拆箱问题是Java中一个老生常谈的问题了,今天我们就来一些看一下装箱和拆箱中的若干问题.本文先讲述装箱和拆箱最基本的东西,再来看一下面试笔试中经常遇到的与装箱 ...
- 【转】深入剖析Java中的装箱和拆箱
深入剖析Java中的装箱和拆箱 自动装箱和拆箱问题是Java中一个老生常谈的问题了,今天我们就来一些看一下装箱和拆箱中的若干问题.本文先讲述装箱和拆箱最基本的东西,再来看一下面试笔试中经常遇到的与装箱 ...
- (转)深入剖析Java中的装箱和拆箱
转:https://www.cnblogs.com/dolphin0520/p/3780005.html 深入剖析Java中的装箱和拆箱 自动装箱和拆箱问题是Java中一个老生常谈的问题了,今天我们就 ...
- 深入剖析Java中的装箱和拆箱(转)
自动装箱和拆箱问题是Java中一个老生常谈的问题了,今天我们就来一些看一下装箱和拆箱中的若干问题.本文先讲述装箱和拆箱最基本的东西,再来看一下面试笔试中经常遇到的与装箱.拆箱相关的问题. 以下是本文的 ...
- Java基础——深入剖析Java中的装箱和拆箱
(转自:http://www.cnblogs.com/dolphin0520/p/3780005.html) 自动装箱和拆箱问题是Java中一个老生常谈的问题了,今天我们就来一些看一下装箱和拆箱中的若 ...
- java语法糖3 深入剖析Java中的装箱和拆箱
装箱 在Java SE5之前,如果要生成一个数值为10的Integer对象,必须这样进行: Integer i = new Integer(10); 而在从Java SE5开始就提供了自动装箱的特性, ...
随机推荐
- Stroke
// A simple blur shader, weighted on alphauniform sampler2D texture;void main(){ float radius = 0 ...
- centos安装k8s集群
准备工作 关闭swap,注释swap分区 swapoff -a 配置内核参数,将桥接的IPv4流量传递到iptables的链 cat > /etc/sysctl.d/k8s.conf < ...
- 细述kubernetes开发流程
本文介绍如何对kubernetes进行二次开发,仓库如何管理,git分支如何管理,怎样利用CI去编译与发布以及如何给社区贡献代码等,结合实际例子,望对大家有所帮助. 开发环境构建 Fork 把gith ...
- 惨,给Go提的代码被批麻了
hello大家好,我是小楼. 不知道大家还记不记得我上次找到了一个Go的Benchmark执行会超时的Bug?就是这篇文章<我好像发现了一个Go的Bug?>. 之后我就向Go提交了一个PR ...
- Hive数仓
分层设计 ODS(Operational Data Store):数据运营层 "面向主题的"数据运营层,也叫ODS层,是最接近数据源中数据的一层,数据源中的数据,经过抽取.洗净. ...
- SQL从零到迅速精通【实用函数(1)】
语法是一个编程语言的基础,真的想玩的6得飞起还是要靠自己定义的函数和变量. 1.使用DECLARE语句创建int数据类型的名为@mycounter的局部变量,输入语句如下: DECLARE @myco ...
- 万字长文---关于PKM收集与整理系统的思考和实践
PKM闭环中有一个很重要的环节就是信息输入,包括各种信息来源,例如微信公众号.博客.知乎.RSS等等,因此也就诞生了一大堆稍后读软件,如何真正有效的获取输入而不是做一只仓鼠是需要思考的.最近看了< ...
- MySQL 8.0安装以及初始化错误解决方法
MySQL 8.0 安装配置及错误排查 官网下载 CentOS7环境下的具体安装步骤 初始化MySQL发生错误的解决方法 忘记数据库root密码 官网下载 mysql官网下载链接:https://de ...
- AQS 源码解读之加锁篇
以 ReentrantLock 创建的非公平锁为基础,进行 AQS 全流程的分析. 分析 demo 一共有 A.B.C 三个线程. public class AQSDemo { // 带入一个银行办理 ...
- pyhon反射
一:反射 1.python面向对象中的反射: 通过字符串的形式操作对象相关的属性.python中的一切事物都是对象(都可以使用反射) 2.四个内置方法 hasattr 检测是否含有某属性 getatt ...