Java Integer int == comparison problem

I think I am a veteran, but if I am not careful, I still fall into the hole.

package xcg.util;

public class Test {

	public static void main(String[] args) {
		Integer a=1;
		Integer b=1;
		Integer c=new Integer(1);
		Long d=new Long(1L);
		
		System.out.println("Integer a=1,Integer b=1,a==b?"+(a==b));
		System.out.println("Integer a=1,Integer b=1,a.equals(b)?"+(a.equals(b)));
		System.out.println("Integer a=1,Integer c=new Integer(1),a==c?"+(a==c));
		System.out.println("Integer a=1,Integer c=new Integer(1),a==c?"+(a.equals(c)));
		System.out.println("Integer a=1,Long d=1,a==d?语法错误");
		System.out.println("Integer a=1,Long d=1,a.equals(d)?"+(a.equals(d)));
		System.out.println("我的理解:==对象比较对象,简单类型比较值,对象比较简单类型则比较值,equals是同数据类型内容比较(不同数据类型即使值同但fase,例如同内容的String和StringBuffer比较结果为false),我的最终做法是:==配合.IntegerValue()或LongValue");
		System.out.println("Integer a=1,Long d=1,a.intValue()==d?"+(a.intValue()==d));
	}
}

The result of the operation is:

Integer a=1,Integer b=1,a==b?true
Integer a=1,Integer b=1,a.equals(b)?true
Integer a=1,Integer c=new Integer(1),a= =c?false
Integer a=1,Integer c=new Integer(1),a==c?true
Integer a=1,Long d=1,a==d? Syntax error
Integer a=1,Long d=1 ,a.equals(d)?false
My understanding: == Object compares objects, simple types compare values, objects compare simple types compare values, equals compares the content of the same data type (different data types even if the value is the same, but fase), My final approach is: == cooperate. IntegerValue() or LongValue
Integer a=1, Long d=1, a.intValue()==d? true

 

Guess you like

Origin blog.csdn.net/xcg8818/article/details/105406280