Autoboxing and unboxing

Java 自动装箱与拆箱(Autoboxing and unboxing)

  • 什么是自动装箱拆箱

基本数据类型的自动装箱(autoboxing)、拆箱(unboxing)是自J2SE 5.0开始提供的功能。 

一般我们要创建一个类的对象实例的时候,我们会这样:

 Class a = new Class(parameter);

 当我们创建一个Integer对象时,却可以这样:

 Integer i = 100; (注意:不是 int i = 100; )

实际上,执行上面那句代码的时候,系统为我们执行了:Integer i = Integer.valueOf(100); (感谢@黑面馒头 和 @MayDayIT 的提醒)

此即基本数据类型的自动装箱功能。

 
  • 基本数据类型与对象的差别 

基本数据类型不是对象,也就是使用int、double、boolean等定义的变量、常量。

基本数据类型没有可调用的方法。

eg:  int t = 1;     t.  后面是没有方法滴。

 Integer t =1; t.  后面就有很多方法可让你调用了。

 
  • 什么时候自动装箱

例如:Integer i = 100;

相当于编译器自动为您作以下的语法编译:Integer i = Integer.valueOf(100);

 
  • 什么时候自动拆箱

  自动拆箱(unboxing),也就是将对象中的基本数据从对象中自动取出。如下可实现自动拆箱:

1 Integer i = 10; // 装箱
2   int t = i; // 拆箱,实际上执行了 int t = i.intValue();

  在进行运算时,也可以进行拆箱。 

1 Integer i = 10;
2 System.out.println(i++);

 

  • Integer的自动装箱

复制代码
//在-128~127 之外的数
 Integer i1 =200;  
 Integer i2 =200;          
 System.out.println("i1==i2: "+(i1==i2));                   
 // 在-128~127 之内的数
 Integer i3 =100;  
 Integer i4 =100;  
 System.out.println("i3==i4: "+(i3==i4));
复制代码
    输出的结果是:
    i1==i2: false
    i3==i4: true

 说明:

equals() 比较的是两个对象的值(内容)是否相同。

"==" 比较的是两个对象的引用(内存地址)是否相同,也用来比较两个基本数据类型的变量值是否相等。

 

前面说过,int 的自动装箱,是系统执行了 Integer.valueOf(int i),先看看Integer.java的源码:

?
1
2
3
4
5
6
public static Integer valueOf( int i) {
     if (i >= - 128 && i <= IntegerCache.high)   // 没有设置的话,IngegerCache.high 默认是127
         return IntegerCache.cache[i + 128 ];
     else
         return new Integer(i);
}

  

对于–128到127(默认是127)之间的值,Integer.valueOf(int i) 返回的是缓存的Integer对象(并不是新建对象)

所以范例中,i3 与 i4实际上是指向同一个对象。

而其他值,执行Integer.valueOf(int i) 返回的是一个新建的 Integer对象,所以范例中,i1与i2 指向的是不同的对象。

当然,当不使用自动装箱功能的时候,情况与普通类对象一样,请看下例:

 

1 Integer i3 = new Integer(100);
2 Integer i4 = new Integer(100);
3 System.out.println("i3==i4: "+(i3==i4)); // 显示false

(感谢易之名的提醒O(∩_∩)O~)

 

  • String 的拆箱装箱

先看个例子:

复制代码
1 String str1 ="abc";
2 String str2 ="abc";
3 System.out.println(str2==str1); // 输出为 true
4 System.out.println(str2.equals(str1)); // 输出为 true
5  
6 String str3 = new String("abc");
7 String str4 = new String("abc");
8 System.out.println(str3==str4); // 输出为 false
9 System.out.println(str3.equals(str4)); // 输出为 true
复制代码

    这个怎么解释呢?貌似看不出什么。那再看个例子。

1 String d ="2";
2 String e ="23";
3 e = e.substring(0, 1);
4 System.out.println(e.equals(d)); // 输出为 true
5 System.out.println(e==d); // 输出为 false
第二个例子中,e的初始值与d并不同,因此e与d是各自创建了个对象,(e==d)为false 。
同理可知,第一个例子中的str3与str4也是各自new了个对象,而str1与str2却是引用了同一个对象。
 
 
 参考:
http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html 
http://javarevisited.blogspot.com/2012/07/auto-boxing-and-unboxing-in-java-be.html 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
《Thinking in Java》是由Bruce Eckel撰写的一本Java编程的经典教材,该书以英文语言出版。以下是该书的目录大纲: Chapter 1: Introduction to Objects - Introduction - The progress of abstraction - How objects communicate - Every object has an interface - The process of object-oriented design - Implementation hiding - Inheritance and reuse - Polymorphism and dynamic binding - Summary of object-oriented principles Chapter 2: Everything Is an Object - Primitive types - How the objects are created - Aliasing: All arguments are passed by value - Documentation comments - Controlling access to members of a class - First exposure to Java syntax Chapter 3: Operators - Thinking recursively - Operator precedence - Assignment with objects - Mathematical operators and precedence - Autoboxing and unboxing - Increment and decrement - Java logic operators - Bitwise operators - The instanceof operator - Summary of operators Chapter 4: Control Structures: Part 1 - True and false - The if-else statement - The switch statement - The while statement - The do-while statement - The for statement - Summary of control structures Chapter 5: Control Structures: Part 2 - The break and continue keywords - Foreach and multidimensional arrays - Using the comma operator - The return keyword - Summary of control structures Chapter 6: Initialization & Cleanup - Member initialization - Constructor initialization - Method overloading & generic methods - Order of initialization - Constructor & parameter lists - Garbage collection - The finalize() method - Summary of initialization and cleanup ......(接下去的章节继续)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值