在Java SE5中,为了减少开发人员的工作,Java提供了自动拆箱与自动装箱功能。
自动装箱: 就是将基本数据类型自动转换成对应的包装类。
自动拆箱:就是将包装类自动转换成对应的基本数据类型。
Integer i = 6; //自动装箱 Integer i = new Integer(6)
i = i + 6; // i = new Integer(i.intValue()+6) i.intValue()自动拆箱Integer i=10 可以替代 Integer i = new Integer(10);,这就是因为Java帮我们提供了自动装箱的功能,不需要开发者手动去new一个Integer对象。
/*
* JDK1.5后出现的特性,自动装箱和自动拆箱
* 自动装箱: 基本数据类型,直接变成对象
* 自动拆箱: 对象中的数据变回基本数据类型
* 方便使用
* 自动装箱和拆箱弊端,可能出现空指针异常
*/
public class IntegerDemo {
public static void main(String[] args) {
function_2();
function_1();
}
/*
* 关于自动装箱和拆箱一些题目
*/
public static void function_2(){
Integer i = new Integer(1);
Integer j = new Integer(1);
System.out.println(i==j);// false 对象地址
System.out.println(i.equals(j));// true 继承Object重写equals,比较的对象数据
System.out.println("===================");
Integer a = 500;
Integer b = 500;
System.out.println(a==b);//false
System.out.println(a.equals(b));//true
System.out.println("===================");
//数据在byte范围内,JVM不会从重新new对象
Integer aa = 127; // Integer aa = new Integer(127)
Integer bb = 127; // Integer bb = aa;
System.out.println(aa==bb); //true
System.out.println(aa.equals(bb));//true
}
//自动装箱和拆箱弊端,可能出现空指针异常
@SuppressWarnings("null")
public static void function_1(){
Integer in =null;
//in = null.intValue()+1
in = in + 1;
System.out.println(in);
}
//自动装箱,拆箱的 好处: 基本类型和引用类直接运算
public static void function(){
//引用类型 , 引用变量一定指向对象
//自动装箱, 基本数据类型1, 直接变成了对象
Integer in = 1; // Integer in = new Integer(1)
//in 是引用类型,不能和基本类型运算, 自动拆箱,引用类型in,转换基本类型
//in+1 ==> in.inValue()+1 = 2
// in = 2 自动装箱
in = in + 1;
System.out.println(in);
}
}
/*
ArrayList<Integer> ar = new ArrayList<Integer>();
ar. add(1);
*/
版权声明:本文为weixin_40976389原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。