为什么 java.lang.Number 不实现 Comparable? [英] Why doesn't java.lang.Number implement Comparable?
问题描述
有谁知道为什么 java.lang.Number
没有实现 Comparable
?这意味着您不能使用 Collections.sort
对 Number
进行排序,这在我看来有点奇怪.
Does anyone know why java.lang.Number
does not implement Comparable
? This means that you cannot sort Number
s with Collections.sort
which seems to me a little strange.
发布讨论更新:
感谢所有有用的回复.我最终做了关于这个话题的更多研究.
Thanks for all the helpful responses. I ended up doing some more research about this topic.
对于 java.lang.Number 为何不实现 Comparable 的最简单解释源于对可变性的担忧.
The simplest explanation for why java.lang.Number does not implement Comparable is rooted in mutability concerns.
稍微回顾一下,java.lang.Number
是AtomicInteger
、AtomicLong
、BigDecimal 的抽象超类型
、BigInteger
、Byte
、Double
、Float
、Integer
、Long
和 Short
.在该列表中,AtomicInteger
和 AtomicLong
不实现 Comparable
.
For a bit of review, java.lang.Number
is the abstract super-type of AtomicInteger
, AtomicLong
, BigDecimal
, BigInteger
, Byte
, Double
, Float
, Integer
, Long
and Short
. On that list, AtomicInteger
and AtomicLong
to do not implement Comparable
.
深入研究,我发现在可变类型上实现 Comparable
不是一个好习惯,因为对象可能在比较期间或之后发生变化,从而导致比较结果无用.AtomicLong
和 AtomicInteger
都是可变的.API 设计者有先见之明,不让 Number
实现 Comparable
,因为它会限制未来子类型的实现.事实上,AtomicLong
和 AtomicInteger
是在 java.lang.Number
最初实现之后很久才添加到 Java 1.5 中的.
Digging around, I discovered that it is not a good practice to implement Comparable
on mutable types because the objects can change during or after comparison rendering the result of the comparison useless. Both AtomicLong
and AtomicInteger
are mutable. The API designers had the forethought to not have Number
implement Comparable
because it would have constrained implementation of future subtypes. Indeed, AtomicLong
and AtomicInteger
were added in Java 1.5 long after java.lang.Number
was initially implemented.
除了可变性之外,这里可能还有其他考虑因素.Number
中的 compareTo
实现必须将所有数值提升为 BigDecimal
,因为它能够容纳所有 Number
> 子类型.我有点不清楚这种提升在数学和表现方面的含义,但我的直觉发现该解决方案很笨拙.
Apart from mutability, there are probably other considerations here too. A compareTo
implementation in Number
would have to promote all numeric values to BigDecimal
because it is capable of accommodating all the Number
sub-types. The implication of that promotion in terms of mathematics and performance is a bit unclear to me, but my intuition finds that solution kludgy.
推荐答案
值得一提的是下面的表达式:
It's worth mentioning that the following expression:
new Long(10).equals(new Integer(10))
总是 false
,这往往会在某些时候绊倒每个人.因此,您不仅不能比较任意的 Number
,而且您甚至无法确定它们是否相等.
is always false
, which tends to trip everyone up at some point or another. So not only can you not compare arbitrary Number
s but you can't even determine if they're equal or not.
此外,对于真正的原始类型(float
、double
),确定两个值是否相等很棘手,必须在可接受的误差范围内完成.试试这样的代码:
Also, with the real primitive types (float
, double
), determining if two values are equal is tricky and has to be done within an acceptable margin of error. Try code like:
double d1 = 1.0d;
double d2 = 0.0d;
for (int i=0; i<10; i++) {
d2 += 0.1d;
}
System.out.println(d2 - d1);
你会留下一些细微的差别.
and you'll be left with some small difference.
回到制作Number
Comparable
的问题.你将如何实施它?使用诸如 doubleValue()
之类的东西不会可靠地做到这一点.记住 Number
子类型是:
So back to the issue of making Number
Comparable
. How would you implement it? Using something like doubleValue()
wouldn't do it reliably. Remember the Number
subtypes are:
字节
;短
;整数
;长
;AtomicInteger
;AtomicLong
;浮动
;双倍
;BigInteger
;和BigDecimal
.
Byte
;Short
;Integer
;Long
;AtomicInteger
;AtomicLong
;Float
;Double
;BigInteger
; andBigDecimal
.
您能否编写一个可靠的 compareTo()
方法,该方法不会演变为一系列 if instanceof 语句?Number
实例只有六个可用的方法:
Could you code a reliable compareTo()
method that doesn't devolve into a series of if instanceof statements? Number
instances only have six methods available to them:
byteValue()
;shortValue()
;intValue()
;longValue()
;floatValue()
;和doubleValue()
.
byteValue()
;shortValue()
;intValue()
;longValue()
;floatValue()
; anddoubleValue()
.
所以我猜 Sun 做出了(合理的)决定,即 Number
仅与它们自身的实例 Comparable
.
So I guess Sun made the (reasonable) decision that Number
s were only Comparable
to instances of themselves.
这篇关于为什么 java.lang.Number 不实现 Comparable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!