The difference between Integer and Int

Integer is a packaging class of int, int is a basic data type

  • Integer is a reference data type, and Int is a basic data type, so Integer must be instantiated before it can be used. Int variables can be used directly.
    The default initial value of Integer is null , and the default initial value of int is 0 .

Attach:


Integer i = new Integer(10);
Integer j = new Integer(10);
System.out.print(i == j); 
false
因为Integer是引用数据类型,所以比较的是对象的地址,对象地址不相等.


Integer i = new Integer(1);
int j = 1;
System.out.print(i == j); 
true
一个为对象,一个为基本类型,此时包装类Integer自动装箱,
比较两个变量结果,结果相同就为真.

Guess you like

Origin blog.csdn.net/SwaeLeeUknow/article/details/109550670