public class test { public static void main(String[] args) { // TODO Auto-generated method stub /** * long 是基本类型 * Long是对象类型,进行比较时:若验证相等则取地址,数值为(-128~127)则相等, * 因为这段数值取的是相同的地址,其余的则不相等,验证相等可用longValue(),可用equals(); */ long a1 = 127; long b1 = 127; long a2 = 123456; long b2 = 123456; Long c1 = 127L; Long d1 = 127L; Long c2 = 123456L; Long d2 = 123456L; System.out.println(a1==b1);//true System.out.println(a2==b2);//true System.out.println(c1==d1);//数值为(-128~127)则相等true, System.out.println(c2==d2);//false 数值不在(-128~127) System.out.println(c2.equals(d2));//true数值不在(-128~127) 可用equals(); System.out.println(a1==c1);//true System.out.println(a2==c2);//true } }
转自:https://www.cnblogs.com/linjiaxin/p/7571759.html
用.longValue()解决