其一,int和Integer之间的比较是拆箱之后数值的比较;
@Test public void testIntAndInteger(){ int i1 = 12; int i2 = 12323; Integer num1 = 12; Integer num2 = new Integer(12323); System.out.println(i1 == num1);//true System.out.println(i2 == num2);//ture }
其二,Integer之间的比较分两种
-128-127之内,通过num1 = 数值或者Integer.valueOf(数值)初始化得到的数值都是相同的;
@Test public void testInteger1(){ Integer num1 = 12; Integer num2 = 12; Integer num3 = Integer.valueOf(12); Integer num4 = new Integer(12); System.out.println(num1 == num2);//true System.out.println(num1 == num3);//true System.out.println(num1 == num4);//false }
-128-127之外,都不相等;
@Test public void testInteger2(){ Integer num1 = 128; Integer num2 = 128; Integer num3 = Integer.valueOf(128); Integer num4 = new Integer(128); System.out.println(num1 == num2);//false System.out.println(num1 == num3);//false System.out.println(num1 == num4);//false }
其三,new出来的元素与常量池无关,相比较都是false,见上;
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)