public class IntegerDemo {
/***
* 1.基本类型 只要数值相等,== 就为 true;
* 2.包装类型 new 的对象,是新建实例放在 堆中.
* 3.包装类型 字面量 获取 在-128 ~ 127 之间,不会新建对象,使用常量池中的对象.超过就会new 新实例.
* 4.int和Integer之间存在自动装箱和拆箱的过程.在-128 ~ 127 之间,不会新建对象,使用常量池中的对象.超过就会new 新实例.
*
*
* */
public static void main(String[] args) {
//基本数据类型
int a =100;
int b =100;
int c =200;
int c2 =200;
System.out.println("基本数据类型");
System.out.println(a==b); //true
System.out.println(c==c2); //true
//包装类型
Integer e = 100;
Integer f = 100;
Integer g = 200;
Integer h = 200;
System.out.println("包装类型类型");
System.out.println(e==f);//true
System.out.println(g==h);//false 警惕!!!! -128 ~ 127 之内不需要new对象,超过需要
//new 实例
Integer i =new Integer(100);
Integer j =new Integer(100);
Integer k =new Integer(200);
Integer l =new Integer(200);
System.out.println("包装类型类型 new实例");
System.out.println(i==j);//false
System.out.println(k==l); //false
//自动拆装箱
Integer a100 =a;
Integer c200 =c;
System.out.println("包装类型类型 自动拆装箱");
System.out.println(a100==e);//true
System.out.println(c200==h); //false -128 ~ 127 之内不需要new对象,超过需要
}
}
打印结果
基本数据类型
true
true
包装类型类型
true
false
包装类型类型 new实例
false
false
包装类型类型 自动拆装箱
true
false