JDK1.5新特性(四)……Autoboxing/Unboxing

援引

Autoboxing/Unboxing - This facility eliminates the drudgery of manual conversion between primitive types (such as int) and wrapper types (such as Integer).

用法

基本数据类型的自动拆箱和自动装箱

autoboxing & unboxing

把一个基本数据类型直接赋给对应的包装类变量, 或者赋给 Object 类型的变量,这个过程称之为自动装箱。

自动拆箱与自动装箱与之相反,即把包装类对象直接赋给一个对应的基本类型变量。

例子

   1: public static void main(String[] args) {
   2:         // TODO Auto-generated method stub
   3:         Integer a = 1;//自动装箱
   4:         int b = new Integer(1);//自动拆箱
   5:         System.out.println(a+"::"+b);
   6:     }

这样的特性省去了过去基本数据类型到对应包装类繁琐的转化过程

原文地址:https://www.cnblogs.com/ShawnWithSmallEyes/p/3453968.html