首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Integer 与 Long 数字类型的比较:Java与Kotlin的细节不同

Integer 与 Long 数字类型的比较:Java与Kotlin的细节不同

作者头像
一个会写诗的程序员
发布2018-08-17 14:49:02
2.6K0
发布2018-08-17 14:49:02
举报

编程语言还是比较傻的。

我们在数学中,123 == 123 , 直觉上是一目了然的。但是到了计算机编程语言中, 问题就显得有点“傻瓜”化了。

值得一提的下面的表达式:

new Long(10).equals(new Integer(10))

始终是 false,这确实是一个违背数学常理的“坑”。

再比如,在Java中

    static void test2() {
        // Integer的自动拆装箱的陷阱(整型数-128到127的值比较问题)
        out.println("-------------------");
        Integer x = new Integer(123);
        Long y = new Long(123);
        //out.println(x == y); // Error:(43, 23) java: incomparable types: java.lang.Integer and java.lang.Long
        out.println(x.equals(y)); // false

        out.println("-------------------");
        Integer c = Integer.valueOf(128);
        Long d = Long.valueOf(128);
        //System.out.println(c == d);//Error:(49, 30) java: incomparable types: java.lang.Integer and java.lang.Long
        System.out.println(c.equals(d)); // false
    }

返回的都是false。 因为这个equals方法实现的逻辑还是僵化的计算机编程逻辑。(注意 “数据类型” 这个概念)

    public boolean equals(Object obj) {
        if (obj instanceof Integer) {
            return value == ((Integer)obj).intValue();
        }
        return false;
    }

    public boolean equals(Object obj) {
        if (obj instanceof Long) {
            return value == ((Long)obj).longValue();
        }
        return false;
    }

是不是有点傻?

还好,有个Comparable接口:

public final class Long extends Number implements Comparable<Long> {}
public final class Integer extends Number implements Comparable<Integer> {}

但是,为什么 java.lang.Number 自己不实现 Comparable 呢?如果实现了,我们不就能进行排序 Number 与 Collections.sort,似乎有点奇怪。

此外,与真正基元类型 (float,double) 确定如果两个值相等,也很棘手,要做一个可接受的误差幅度内。请尝试如下代码:

double d1 = 1.0d;
double d2 = 0.0d;
for (int i=0; i<10; i++) {
  d2 += 0.1d;
}
System.out.println(d2 - d1); // -1.1102230246251565E-16, 浮点数会有误差

和你会留下一些小的差别。

所以回到这一问题作出的 Number Comparable。您将如何实施?使用类似 doubleValue() 不可靠。请记住,Number 子类型是:

Byte;
Short;
Integer;
Long;
AtomicInteger;
AtomicLong;
Float;
Double;
BigInteger;和
BigDecimal.

可能您代码可靠 compareTo() 为一系列的如果不下放的方法假如语句吗?Number 实例只能有六种方法向他们提供:

byteValue();
shortValue();
intValue();
longValue();
floatValue();和
doubleValue().

看到了吧,计算机总是那么“傻”,不像人脑那样“智能”。

在Long.java中,提供了一个compareTo方法

    public int compareTo(Long anotherLong) {
        return compare(this.value, anotherLong.value);
    }

但是,仍然局限在Long类型之间比较。就是说,下面的代码 Error:(53, 33) 依然编译不通过

        Integer c = Integer.valueOf(128);
        Long d = Long.valueOf(128);
        //System.out.println(c == d);//Error:(49, 30) java: incomparable types: java.lang.Integer and java.lang.Long
        out.println(d.equals(c));
        out.println(d.compareTo(c)); // Error:(53, 33) java: incompatible types: java.lang.Integer cannot be converted to java.lang.Long
    }

在Kotlin中,Long类型实现了多个compareTo方法,稍微方便了数字之间的比较

    public operator fun compareTo(other: Byte): Int

    public operator fun compareTo(other: Short): Int

    public operator fun compareTo(other: Int): Int

    public override operator fun compareTo(other: Long): Int

    public operator fun compareTo(other: Float): Int

    public operator fun compareTo(other: Double): Int

Kotlin中,Int类型与Long类型之间比较大小:

package com.easy.kotlin

fun main(args: Array<String>) {
    test1()
}

fun test1() {
    val x: Int = 123
    val y: Int = 123

    println(x == y)
    println(x === y)

    val z: Int = 456
    val w: Int = 456
    println(z == w)
    println(z === w)

    val a: Long = 789
    val b: Int = 1010
    println(a<b)
    //println(a!=b) //Error:(22, 13) Kotlin: Operator '!=' cannot be applied to 'Long' and 'Int'
    //println(a==b) //Error:(23, 13) Kotlin: Operator '==' cannot be applied to 'Long' and 'Int'
    println(a.compareTo(b))
}

输出:

true
 true
 true
 true
 true
 -1

源代码工程:https://github.com/EasyKotlin/kotlin_tutorials

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.09.13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档