您的位置:首页 > 编程语言 > Java开发

java 中long型数据的对比

2015-09-05 23:14 447 查看
在对比之前先看一个程序吧

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

Long a2 = 3l;
Long b2 = 3l;
System.out.println(a2.equals(b2));
Long c2 = 3l;
Long d2 = 3l;
System.out.println(c2.longValue()==d2.longValue());
System.out.println(c2==d2);

Long c3 = 127l;
Long d3 = 127l;
System.out.println(c3.longValue()==d3.longValue());
System.out.println(c3==d3);
System.out.println(c3.equals(d3));

Long c4 = 129l;
Long d4 = 129l;
System.out.println(c4.longValue()==d4.longValue());
System.out.println(c4==d4);
System.out.println(c4.equals(d4));


输出结果是什么呢:

true

true

true

true

true

true

true

true

true

false

true

那个129l的为什么不能直接==比较呢

这个现在我也不太明白

long型数据不能直接==进行比较 可以通过math.abs(a-b)<0.000001

也可以.equals,也可以longValue在比较
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: