两个Long类型怎么比较大小啊? 能具体举个例子吗?

来源:百度知道 编辑:UC知道 时间:2024/06/30 19:20:23
两个Long类型怎么比较大小啊? 能具体举个例子吗?
我用equals判断是否相等都不行!!

Long a

Long b
a.compareTo(b);返回一个int:
0:the value 0 if this Long is equal to the argument Long;
负数:a value less than 0 if this Long is numerically less than the argument Long; and
正数:a value greater than 0 if this Long is numerically greater than the argument Long (signed comparison).
9年前的问题,啧啧,有意思。

Java中如果使用 == 双等于比较对象,等于比较的是两个对象的内存地址,也就是比较两个对象是否是同一个对象
如果比较两个Long对象值是否相等,则不可以使用双等号进行比较,可以采用如下方式:
1. 使用 equals 方法
Long a = new Long(3);
Long b = new Long(3);
System.out.println(a.equals(b));
2. 使用 Long 类型中的 longValue() 方法进行比较,如

Long a = new Long(3);
Long b = new Long(3);
System.out.println(a.longValue()==b.longValue());