public static void main(String[] args) {
Long goodsId=127L;
Long itemId=127L;
//true
System.out.println(goodsId == itemId);
goodsId=128L;
itemId=128L;
//false
System.out.println(goodsId == itemId);
Long x=new Long(127);
Long y=new Long(127);
//false
System.out.println(x == y);
//true
System.out.println(x.equals(y));
}
Long是引用類型,要比較兩個Long的大小,一定要用equals而不能用==
但是,當Long為常量且常量值小於一個字節時,兩個Long指向同一個常量內容;
當Long為常量且常量值大於一個字節時,兩個Long指向不同的常量內容。
最后總結,引用比較一定要用equals而不要用==