Demo.java:
package cn.xxx.demo;
/*
* JDK1.5后出现的特性,自动装箱和自动拆箱
* 自动装箱: 基本数据类型,直接变成对象(引用类型)
* 自动拆箱: 对象中的数据变回基本数据类型
*/
public class Demo {
public static void main(String[] args) {
function_2();
}
/*
* 关于自动装箱和拆箱一些题目
*/
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; // 大于127, 不在byte范围内。
Integer b = 500;
System.out.println(a==b);//false
System.out.println(a.equals(b));//true
System.out.println("===================");
//数据在byte范围内(-128~127),JVM不会重新new对象。 byte范围内的数据在JVM中只存在一份。
Integer aa = 127; // Integer aa = new Integer(127)
Integer bb = 127; // Integer bb = aa; 127在byte范围内,128不在范围内
System.out.println(aa==bb); //true
System.out.println(aa.equals(bb));//true
}
//自动装箱和拆箱弊端,可能出现空指针异常
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.intValue()+1 = 2
// in = 2 自动装箱
in = in + 1; // 先自动拆箱,运算,再自动装箱
System.out.println(in);
}
}
/*
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1); // 自动装箱 1=>Integer
*/