編程語言還是比較傻的财搁。
我們在數(shù)學(xué)中,123 == 123 哑舒, 直覺上是一目了然的妇拯。但是到了計算機編程語言中, 問題就顯得有點“傻瓜”化了洗鸵。
值得一提的下面的表達式:
new Long(10).equals(new Integer(10))
始終是 false越锈,這確實是一個違背數(shù)學(xué)常理的“坑”。
再比如膘滨,在Java中
static void test2() {
// Integer的自動拆裝箱的陷阱(整型數(shù)-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方法實現(xiàn)的邏輯還是僵化的計算機編程邏輯。(注意 “數(shù)據(jù)類型” 這個概念)
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 自己不實現(xiàn) Comparable 呢?如果實現(xiàn)了躲胳,我們不就能進行排序 Number 與 Collections.sort蜓洪,似乎有點奇怪。
此外坯苹,與真正基元類型 (float隆檀,double) 確定如果兩個值相等,也很棘手粹湃,要做一個可接受的誤差幅度內(nèi)恐仑。請嘗試如下代碼:
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, 浮點數(shù)會有誤差
和你會留下一些小的差別。
所以回到這一問題作出的 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類型實現(xiàn)了多個compareTo方法,稍微方便了數(shù)字之間的比較
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