java 封箱 拆箱_【细说Java】Java封箱拆箱的一些问题

1.概念

首先简单介绍一下概念性的东西:

所谓封箱:就是把基本类型封装成其所对应的包装类型;

而拆箱则恰好相反,是把包装类型转换成其所对应的基本数据类型。

如基本类型int,封箱后的包装类是Integer。

2.包装类的缓存值,equals与==

相信,大家对equals与==一定很熟悉了吧,同样,包装类也重写了equals方法,只要两个包装类对象所包装的基本类型数据是相等的,那么,则认为两个包装类对象是相等的;

然后,看一段简单的程序,并预测以下它的打印结果:

public classTest {public static voidmain(String[] args) {

System.out.println("---------------------");

testBooleanBoxing();

System.out.println("---------------------");

testByteBoxing();

System.out.println("---------------------");

testShortBoxing();

System.out.println("---------------------");

testIntegerBoxing();

System.out.println("---------------------");

testFloatBoxing();

}public static voidtestFloatBoxing() {

Float float1=10F;

Float float2=10F;

Float float3=300F;

Float float4=300F;

System.out.println("float1.equals(float2) : " +float1.equals(float2));

System.out.println("float1 == float2 : " + (float1 ==float2));

System.out.println("float3.equals(float4) : " +float3.equals(float4));

System.out.println("float3 == float4 : " + (float3 ==float4));

}public static voidtestIntegerBoxing() {

Integer int1= 10;

Integer int2= 10;

Integer int3= 300;

Integer int4= 300;

System.out.println("int1.equals(int2) : " +int1.equals(int2));

System.out.println("int1 == int2 : " + (int1 ==int2));

System.out.println("int3.equals(int4) : " +int3.equals(int4));

System.out.println("int3 == int4 : " + (int3 ==int4));

}public static voidtestByteBoxing() {

Byte byte1= -128;

Byte byte2= -128;

Byte byte3= 127;

Byte byte4= 127;

System.out.println("byte1.equals(byte2) : " +byte1.equals(byte2));

System.out.println("byte1 == byte2 : " + (byte1 ==byte2));

System.out.println("byte3.equals(byte4) : " +byte3.equals(byte4));

System.out.println("byte3 == byte4 : " + (byte3 ==byte4));

}public static voidtestShortBoxing() {

Short short1= -128;

Short short2= -128;

Short short3= 128;

Short short4= 128;

System.out.println("short1.equals(short2) : " +short1.equals(short2));

System.out.println("short1 == short2 : " + (short1 ==short2));

System.out.println("short3.equals(short4) : " +short3.equals(short4));

System.out.println("short3 == short4 : " + (short3 ==short4));

}public static voidtestBooleanBoxing() {

Boolean bool1= true;

Boolean bool2= true;

Boolean bool3= true;

Boolean bool4= true;

System.out.println("bool1.equals(bool2) : " +bool1.equals(bool2));

System.out.println("bool1 == bool2 : " + (bool1 ==bool2));

System.out.println("bool3.equals(bool4) : " +bool3.equals(bool4));

System.out.println("bool3 == bool4 : " + (bool3 ==bool4));

}

}

然后看以下结果:

---------------------bool1.equals(bool2) :truebool1== bool2 : truebool3.equals(bool4) :truebool3== bool4 : true

---------------------byte1.equals(byte2) :truebyte1== byte2 : truebyte3.equals(byte4) :truebyte3== byte4 : true

---------------------short1.equals(short2) :trueshort1== short2 : trueshort3.equals(short4) :trueshort3== short4 : false

---------------------int1.equals(int2) :trueint1== int2 : trueint3.equals(int4) :trueint3== int4 : false

---------------------float1.equals(float2) :truefloat1== float2 : falsefloat3.equals(float4) :truefloat3== float4 : false

不知道大家预测的结果和实际结果是否是相同的;

或许有些人有疑问,不同的值对于同一包装类来说,使用 == 运算符得出的结果却是不同的,这是为什么呢?

这就要从包装类的不可变性说起。先说以下String,因为String类的对象创建后不可改变,所以多个引用指向同一个String也不会有什么危险,因此,在编译阶段会将String常量放入字符串常量池中,当下次使用时就可以直接从字符串常量池中提取。

而包装类和String类似,对象同样是不可变的,所以对于某些频繁使用的值,系统提供了包装类的缓存(即预先创建了经常使用的包装类对象,可以参考包装类的valueOf方法),当需要时直接从缓存中提取,而不是再创建一个新的包装类对象。这样当重复使用的时候就可以避免重复创建对象造成的资源浪费。而这些缓存的包装类的值如下:

boolean的所有值(true 和 false);

byte的所有制(-128 ~ 127);

char值的(0 ~ 127);

short值的(-128 ~ 127);

int值的(-128 ~ 127);

long值的(-128 ~ 127);

其中,浮点数类型float和double,包装类没有缓存。

如以上int类型,因为10在Integer的缓存范围内,所以

Integer int1 = 10;

Integer int2= 10;

两次对10的封箱使用的都是缓存中包装数值为10的Integer对象(这些对象被封装在IntegerCache类中),因此int1和int2指向的相同的对象,所以不论是 equals 还是 == ,运算的结果都是true;

而数值300则不在缓存的范围内,因此并不是使用缓存中的对象,而是新建两个Integer对象,所以 int3 和 int4 使用 == 运算符的结果为false,其他的情况与Integer类似。

如果要了解详细的缓存值,可查看源码中相应的Cache即可,如char值的可查看CharacterCache类,long值的可查看LongCache类;

3. 拆箱还是封箱

在Java中 == 和 != 都可以应用于任何类型,但如果这两个二元操作符的两个操作数一个是基本数据类型,而另一个是包装类,那这是会如何操作呢?

int x = 5;

Integer y= 5;boolean bool = (x == y);

如果要计算 x== y,那么,是将x封箱成Integer类型与y进行比较,还是将y拆箱成int类型与x进行比较呢?

我们看下面代码:

public static voidmain(String[] args) {int int1 = 200;

Integer int2= 200;if (int1 ==int2) {

System.out.println("拆箱操作");

}else{

System.out.println("封箱操作");

}int int3 = 10;

Integer int4= new Integer(10);if (int3 ==int4) {

System.out.println("拆箱操作");

}else{

System.out.println("封箱操作");

}//混合类型比较

short short1 = 200;

Integer int5= 200;if (short1 ==int5) {

System.out.println("拆箱操作");

}else{

System.out.println("封箱操作");

}

}

返回结果如下:

拆箱操作

拆箱操作

拆箱操作

结果已经一目了然了。当基本数据类型与包装类型进行 == 比较时,因为比较的是内存地址,所以根据结果来看,是将包装类拆箱成基本数据类型,然后对两个基本数据类型进行比较。

4. 方法中的封箱与拆箱操作

看下面的代码:

public classTest {public static voidmain(String[] args) {

Test test= newTest();int x = 10;

test.box1(x);

test.box2(x);

test.box3(x);

}public void box1(intx) {

System.out.println("调用int参数的box1方法");

}public void box1(floatx) {

System.out.println("调用float参数的box1方法");

}public voidbox1(Integer x) {

System.out.println("调用Integer参数的box1方法");

}public void box2(floatx) {

System.out.println("调用float参数的box2方法");

}public voidbox2(Integer x) {

System.out.println("调用Integer参数的box2方法");

}public voidbox3(Integer x) {

System.out.println("调用Integer参数的box3方法");

}

}

运行后,看以下结果:

调用int参数的box1方法

调用float参数的box2方法

调用Integer参数的box3方法

结果应该不会有什么问题,重载方面的问题我会单独拿出来再说。这里要说的是,把封箱操作置后考虑(即只有当选择不到合适参数的方法时,才考虑封箱操作),主要是为了兼容以前的版本。试想:

public void method(floatf) {}public void method(Integer i) {}

如果要调用 method(7),那么在JDK5之前的版本,因为没有自动封箱,所以会调用float参数的method方法。这是大多数人都知道的,如果JDK5后规定优先考虑封箱操作的话,那么该程序就会调用Integer参数的method方法,这对大多数人来说,都是很尴尬的,以前的那些脑海里的规则都要改变了。所以,对于新增的自动封箱特性,只能置后考虑了。

参考自:《细说Java》

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值