long 128L == long 128L 值为false

Long的范围为-128~127当数值超过这个值时,就会new方法生成新的对象,而用等号比较的是地址,所以long 128L == long 128L 为false

Long值的比较一般通过 Long.equals() 或Long.longVale()进行比较

public class demo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Long long1 = 127L;
        Long long2 = 127L;
        
        Long long3 = 128L;
        Long long4 = 128L;
        
        
        System.out.println("long1 == long2 : " + (long1 == long2)); // true
        
        System.out.println("long3 == long4 : " + (long3 == long4)); // false
        System.out.println("long3.longValue() == long4.longValue() : " + (long3.longValue() == long4.longValue())); // true
        System.out.println("long3.equals(long4): " + long3.equals(long4));   //true
    }

}

 

版权声明:本文为xiaoqi58585945原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/xiaoqi58585945/p/9394152.html