Java的Long型比较大小:大于和小于号会自动拆箱,等于号则不会

22 篇文章 4 订阅

自动拆箱原理:包装类会自动转换为基本类型。

满足下面两个条件

1.传参给基本类型

2.赋值给基本类型

参考:

1.https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

2.https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.8

3.https://docs.oracle.com/javase/8/docs/api/index.html

java中Long型值比较,用==去比较值是不行的,但是>和<可以。等于在很多文章都有说过,大于和小于没人说。

示例代码如下:

package com.vvvtimes.server;

public class autoUnboxing {
    public static void main(String[] args) {
        //1.Long重写了hashcode 值为(int)(value ^ (value >>> 32)
        //2.System.identityHashCode()可获取原始hashcode,无论hashcode有没有重写
        //3.Long的equals重写了方法,比较的是longValue Object比较的是this == obj,也是比较原始hashcode
        //4.Object 在API里说明 如果重写了hashCode方法,也需要重写equals方法
        //5.compareTo方法 实际调用Long.value
        //6.<128 int能存储的会使用LongCache,原始hashcode一样。
        //7. == 比较的是原始hashcode,< >比较的是自动拆箱的值

        Long a = 21432433L;
        Long b = 21432434L;
        System.out.println("a的hashcode:" + a.hashCode()+" 原始HashCode:"+Integer.toHexString(System.identityHashCode(a)));
        System.out.println("b的hashcode:" + b.hashCode()+" 原始HashCode:"+Integer.toHexString(System.identityHashCode(b)));
        System.out.println(a.equals(b));
        System.out.println(a.compareTo(b));
        System.out.println(a == b);
        System.out.println(a.longValue() == b.longValue());
        System.out.println(a > b);
        System.out.println(a.longValue() > b.longValue());
        System.out.println(a < b);
        System.out.println(a.longValue() < b.longValue());
        System.out.println("=============分割线1=====================");


        Long c = 214324324L;
        Long d = 214324324L;
        System.out.println("c的hashcode:" + c.hashCode()+" 原始HashCode:"+Integer.toHexString(System.identityHashCode(c)));
        System.out.println("d的hashcode:" + d.hashCode()+" 原始HashCode:"+Integer.toHexString(System.identityHashCode(d)));
        System.out.println(c.equals(d));
        System.out.println(c.compareTo(d));
        System.out.println(c == d);
        System.out.println(c.longValue() == d.longValue());
        System.out.println(c > d);
        System.out.println(c.longValue() > d.longValue());
        System.out.println(c < b);
        System.out.println(c.longValue() < d.longValue());
        System.out.println("=============分割线2=====================");

        //LongCache验证
        Long e = 123L;
        Long f = 123L;
        System.out.println("e的hashcode:" + e.hashCode()+" 原始HashCode:"+Integer.toHexString(System.identityHashCode(e)));
        System.out.println("f的hashcode:" + f.hashCode()+" 原始HashCode:"+Integer.toHexString(System.identityHashCode(f)));
        System.out.println(e.equals(f));
        System.out.println(e.compareTo(f));
        System.out.println(e == f);
        System.out.println(e.longValue() == f.longValue());
        System.out.println(e > f);
        System.out.println(e.longValue() > f.longValue());
        System.out.println(e < f);
        System.out.println(e.longValue() < f.longValue());
        System.out.println("=============END=====================");
    }
}

运行结果如下

a的hashcode:21432433 原始HashCode:7a79be86
b的hashcode:21432434 原始HashCode:34ce8af7
false
-1
false
false
false
false
true
true
=============分割线1=====================
c的hashcode:214324324 原始HashCode:b684286
d的hashcode:214324324 原始HashCode:880ec60
true
0
false
true
false
false
false
false
=============分割线2=====================
e的hashcode:123 原始HashCode:7f63425a
f的hashcode:123 原始HashCode:7f63425a
true
0
true
true
false
false
false
false
=============END=====================

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值