Java - int和Integer有什么区别?

版权声明:欢迎转载并请注明出处,谢谢~~ https://blog.csdn.net/chimomo/article/details/78342294

Java是一个近乎纯洁的面向对象编程语言,但是为了编程的方便还是引入了基本数据类型,为了能够将这些基本数据类型当成对象操作,Java为每一个基本数据类型都引入了对应的包装类型(wrapper class),int的包装类就是Integer,从Java 5开始引入了自动装箱/拆箱机制,使得二者可以相互转换。
Java 为每个原始类型提供了包装类型:
- 原始类型: boolean,char,byte,short,int,long,float,double

- 包装类型:Boolean,Character,Byte,Short,Integer,Long,Float,Double

class AutoUnboxingTest {
 
    public static void main(String[] args) {
        Integer a = new Integer(1);
        Integer b = 1;                  // 将1自动装箱成Integer类型
        int c = 1;
        System.out.println(a == b);     // false,两个引用没有引用同一对象
        System.out.println(a == c);     // true,a自动拆箱成int类型再和c比较
    }
}

猜你喜欢

转载自blog.csdn.net/chimomo/article/details/78342294