java的==比较规律?

双等号左面是引用类型,右面是基本类型怎么作比较的?
就像System.out.println(new Integer(1200) == 1200);

基本类型与对应的包装类型进行比较时,比较的是值是否相等。

如果包装类型解包后的值与基本类型的值相等,则==比较的结果就是真。

下面是一个用于验证的例子:

public class Test {

public static void main(String []args) {

Integer it1=1200;

Integer it2=new Integer(1200);

int i=1200;

System.out.println(it1==it2);

System.out.println(it1==1200);

System.out.println(it2==1200);

System.out.println(it1==i);

System.out.println(it2==i);

}

}

输出结果

追问

追问一下,我说的不是这个意思,我的意思是说,作比较时是把int转换为Integer还是将Integer转为int

追答

将Integer转换成int

温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-08-26
Integer 是 int 的包装类, ==比较的时候, Integer 会自动拆箱成 int 比较