一:什么是自动装箱拆箱
装箱
就是自动将基本数据类型转换为包装器类型;
拆箱
就是自动将包装器类型转换为基本数据类型。
java中需要装箱拆箱的类型如下:
基本数据类型 | 包装器类型 |
---|---|
int(4字节) | Integer |
byte(1字节) | Byte |
short(2字节) | Short |
long(8字节) | Long |
float(4字节) | Float |
double(8字节) | Double |
char(2字节) | Character |
boolean(未定) | Boolean |
二:测试代码分析
public class IntegerTest {
public static void main(String[] args) {
Integer a = new Integer(10);
int b = 10;
System.out.println(a == b);
}
}
结果打印:
true
执行反编译:javap -c IntegerTest
E:\WorkSpace\Git\sortalgorithm-demos\target\classes\com\leo\demo\othertest>javap -c IntegerTest
警警告告: 二二进进制制文文件件IntegerTest包包含含com.leo.demo.othertest.IntegerTest
Compiled from "IntegerTest.java"
public class com.leo.demo.othertest.IntegerTest {
public com.leo.demo.othertest.IntegerTest();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: new #2 // class java/lang/Integer
3: dup
4: sipush 220
7: invokespecial #3 // Method java/lang/Integer."<init>":(I)V
10: astore_1
11: sipush 220
14: istore_2
15: getstatic #4 // Field java/lang/System.out:Ljava/io/PrintStream;
18: aload_1
19: invokevirtual #5 // Method java/lang/Integer.intValue:()I
22: iload_2
23: if_icmpne 30
26: iconst_1
27: goto 31
30: iconst_0
31: invokevirtual #6 // Method java/io/PrintStream.println:(Z)V
34: return
}
以上转换使用了
Method java/lang/Integer.intValue:()
此方法的源码如下:
/**
* 这个方法会缓存-128到127的数的对象,如果不在这个区间则重新生成新的对象
* This method will always cache values in the range -128 to 127,
*/
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
三:测试装拆箱缓存对象
根据上面的分析,如果是使用valueOf方法则会缓存-128—127的对象,那做如下测试:
public class IntegerTest {
public static void main(String[] args) {
Integer a1 = 10;
Integer a2 = 10;
Integer a3 = new Integer(10);
Integer a4 = new Integer(10);
System.out.println("a1 == a2 返回 "+(a1 == a2));
System.out.println("a1 == a3 返回 "+(a1 == a3));
System.out.println("a3 == a4 返回 "+(a3 == a4));
System.out.println("------分割线------");
Integer c1 = 200;
Integer c2 = 200;
Integer c3 = new Integer(200);
Integer c4 = new Integer(200);
System.out.println("c1 == c2 返回 "+(c1 == c2));
System.out.println("c1 == c3 返回 "+(c1 == c3));
System.out.println("c3 == c4 返回 "+(c3 == c4));
}
}
打印结果:
a1 == a2 返回 true
a1 == a3 返回 false
a3 == a4 返回 false
------分割线------
c1 == c2 返回 false
c1 == c3 返回 false
c3 == c4 返回 false
结论,如果使用Integer a1 = 10;这种方式使用装箱操作使用的就是valueOf方法实现的。当-128—127时返回的是同一对象,不在此范围的生成了新的对象。
四:其他包装类缓存范围
归类:
Integer
系列:Integer、Short、Byte、Character、Long这几个类的valueOf方法的实现是类似的。
Double
系列:Double、Float的valueOf方法的实现是类似的。每次都返回不同的对象。
针对Integer总结缓存范围,如下:
类型 | 相同对象范围 |
---|---|
Integer | [-128,127] |
Short | [-128,127] |
Character | c<=127 |
Long | [-128,127] |
最后注意,比较两个包装类型数值大小
要使用equals
方法
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/72709.html