-128~127的范围,int和Integer用"=="或"equals"比较相同数值时,都是true;
<-128或>127情况:
Integer和int用"=="或"equals"比较相同数值时,都是true;Integer和Integer用"equals"比较相同数值时,都是true;Integer和Integer用"=="比较相同数值时,都是false; /** * @author: alen.wang * @date: 2020-09-03 11:20 */ public class Test { public static void main(String[] args) { Integer itr1 = 127; Integer itr11 = 127; Integer itr2 = 128; Integer itr3 = 128; Integer itr4 = -128; Integer itr44 = -128; Integer itr5 = -129; Integer itr6 = -129; int i1 = 127; int i2 = 128; int i3 = 128; int i4 = -128; int i5 = -129; System.out.println("-128~127的范围Integer&int,'=='比较:" + (itr1 == i1) + " : " + (itr4 == i4)); System.out.println("-128~127的范围Integer&Integer,'=='比较:" + (itr1 == itr11) + " : " + (itr4 == itr44)); System.out.println(">127的范围Integer&int,'equals'比较:" + (itr2.equals(i2))); System.out.println(">127的范围Integer&Integer,'equals'比较:" + (itr2.equals(itr3))); System.out.println("<-128的范围Integer&int,'equals'比较:" + (itr5.equals(i5))); System.out.println(">127的范围Integer&Integer,'=='比较:" + (itr2 == itr3)); System.out.println("<-128的范围Integer&Integer,'=='比较:" + (itr5 == itr6)); } } 结果输出: -128~127的范围Integer&int,'=='比较:true : true -128~127的范围Integer&Integer,'=='比较:true : true >127的范围Integer&int,'equals'比较:true >127的范围Integer&Integer,'equals'比较:true <-128的范围Integer&int,'equals'比较:true >127的范围Integer&Integer,'=='比较:false <-128的范围Integer&Integer,'=='比较:false