Long的比較要用equals而不要用==
當Long為常量且常量值小於一個字節(<=127)時,兩個Long指向同一個常量內容;
Long userId=127L; Long authorId=127L; System.out.println(userId==authorId);//true
當Long為常量且常量值大於一個字節(>127)時,兩個Long指向不同的常量內容。
Long userId=128L; Long authorId=128L; System.out.println(userId==authorId);//false
當Long是引用類型時,比較兩個Long的大小,一定要用equals而不能用==
Long x=new Long(18); Long y=new Long(18); System.out.println(x==y);//false System.out.println(x.equals(y));//true
原因:Java 基本類型的包裝類的大部分都實現了常量池技術,即Byte,Short,Integer,Long,Character;這5種包裝類默認創建了數值[-128,127]的相應類型的緩存數據,但是超出此范圍仍然會去創建新的對象。