Comparable接口,怪。。。
JDK文档中,java.lang.Comparable中有:
Virtually all Java core classes that implement comparable have natural orderings that are consistent with equals. One exception is java.math.BigDecimal, whose natural ordering equates BigDecimal objects with equal values and different precisions (such as 4.0 and 4.00).
但是
import java.math.BigDecimal;
class Test {
public static void main(String[] args) {
BigDecimal b1 = new BigDecimal(4.0);
BigDecimal b2 = new BigDecimal(4.00);
if(b1.equals(b2)) System.out.println("equals");
if(b1.compareTo(b2)==0) System.out.println("equals");
}
}
结果都打印出equals,为什么?