Double源码阅读(1.8)

448 阅读2分钟

之前阅读了Float的源码:juejin.cn/post/695325… 今天来看看Double的源码有什么区别?

类定义

public final class Double extends Number implements Comparable<Double>
public abstract class Number implements java.io.Serializable
  • 看类定义和Float基本一样,也是继承了Number类、实现了Comparable接口,且是final不可继承的。
  • 浮点数的基本知识看之前Float的文章。

成员变量

public static final double POSITIVE_INFINITY = 1.0 / 0.0;
public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
public static final double NaN = 0.0d / 0.0;
public static final double MAX_VALUE = 0x1.fffffffffffffP+1023; // 1.7976931348623157e+308
public static final double MIN_NORMAL = 0x1.0p-1022; // 2.2250738585072014E-308
public static final double MIN_VALUE = 0x0.0000000000001P-1022; // 4.9e-324
public static final int MAX_EXPONENT = 1023;
public static final int MIN_EXPONENT = -1022; //11位 [0-1023] --> [-1023,1024] 全0,全1去掉 [-1022,1023]
public static final int SIZE = 64;
public static final int BYTES = SIZE / Byte.SIZE; //8
public static final Class<Double>   TYPE = (Class<Double>) Class.getPrimitiveClass("double");
private final double value;
private static final long serialVersionUID = -9172774392245257468L;

对比float的成员变量其实没多大区别,除了值得变化。

构造方法

    public Double(double value) {
        this.value = value;
    }
    public Double(String s) throws NumberFormatException {
        value = parseDouble(s);
    }
    public static double parseDouble(String s) throws NumberFormatException {
        return FloatingDecimal.parseDouble(s);
    }

valueOf

    public static Double valueOf(String s) throws NumberFormatException {
        return new Double(parseDouble(s));
    }
    public static Double valueOf(double d) {
        return new Double(d);
    }
    public static double parseDouble(String s) throws NumberFormatException {
        return FloatingDecimal.parseDouble(s);
    }

toString

参考之前Float

Number 父类方法实现

参考之前Float

isXXX

参考之前Float

hashcode

    @Override
    public int hashCode() {
        return Double.hashCode(value);
    }
     public static int hashCode(double value) {
        long bits = doubleToLongBits(value);
        // 想想HashMap里hashcode计算原理一致
        return (int)(bits ^ (bits >>> 32));
    }
    public static long doubleToLongBits(double value) {
        long result = doubleToRawLongBits(value);
        // Check for NaN based on values of bit fields, maximum
        // exponent and nonzero significand.
        if ( ((result & DoubleConsts.EXP_BIT_MASK) ==
              DoubleConsts.EXP_BIT_MASK) &&
             (result & DoubleConsts.SIGNIF_BIT_MASK) != 0L)
            result = 0x7ff8000000000000L;
        return result;
    }
    public static native long doubleToRawLongBits(double value);

比较

    public boolean equals(Object obj) {
        return (obj instanceof Double)
               && (doubleToLongBits(((Double)obj).value) ==
                      doubleToLongBits(value));
    }
    public int compareTo(Double anotherDouble) {
        return Double.compare(value, anotherDouble.value);
    }
    public static int compare(double d1, double d2) {
        if (d1 < d2)
            return -1;           // Neither val is NaN, thisVal is smaller
        if (d1 > d2)
            return 1;            // Neither val is NaN, thisVal is larger

        // Cannot use doubleToRawLongBits because of possibility of NaNs.
        long thisBits    = Double.doubleToLongBits(d1);
        long anotherBits = Double.doubleToLongBits(d2);

        return (thisBits == anotherBits ?  0 : // Values are equal
                (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
                 1));                          // (0.0, -0.0) or (NaN, !NaN)
    }