Why is Java's Double.compare(double, double) implemented the way it is?

后端 未结 4 1160
情书的邮戳
情书的邮戳 2020-12-01 03:26

I was looking at the implementation of compare(double, double) in the Java standard library (6). It reads:

public static int compare(double d1, double d2) {
         


        
4条回答
  •  我在风中等你
    2020-12-01 04:19

    The merit is that it's the simplest code that fulfills the specification.

    One common characteristic of rookie programmers is to overvalue reading source code and undervalue reading specifications. In this case, the spec:

    http://java.sun.com/javase/6/docs/api/java/lang/Double.html#compareTo%28java.lang.Double%29

    ... makes the behavior and the reason for the behavior (consistency with equals()) perfectly clear.

提交回复
热议问题