Skip to content

Latest commit

 

History

History
30 lines (22 loc) · 977 Bytes

2. Auto-boxing & unboxing.md

File metadata and controls

30 lines (22 loc) · 977 Bytes

2. Auto-boxing & unboxing

package com.alexanderli95.autoBoxingAndUnboxing;

/**
 * 测试自动装箱和拆箱
 */
public class Test01 {
    public static void main(String[] args){
        Integer i=1000;   //自动装箱,jdk5.0之后才能这么写,编译器自动将这种代码转为Integer i=new Integer(1000)

        int c=new Integer(1000);  //自动拆箱,编译器转化为了new Integer(1000).intValue();

        Integer c1=1234;
        Integer c2=1234;

        System.out.println(c1==c2);            //false
        System.out.println(c1.equals(c2));     //true

        //[-128,127]之间的数仍然会被当做基本数据类型处理,虽然装箱了,但是运算时仍然当作基本数据类型,小数字用的多,为了提高效率
        Integer c3=123;
        Integer c4=123;
        System.out.println(c3==c4);            //true
        System.out.println(c3.equals(c4));     //true
    }
}