JDK源码之Double类&Float类分析

一 概述

Double 类是基本类型double的包装类,fainl修饰,在对象中包装了一个基本类型double的值。Double继承了Number抽象类,具有了转化为基本double类型的功能。
此外,该类还提供了多个方法,可以将 double 类型与 String 类型相互转换,同时 还提供了处理 double 类型时比较常用的常量和方法。

二 Number类

Number传送门

三 源码解析

    // 表示正无穷大, 注意:浮点数才有无穷的概念,整数是没有的   1/0 会直接报错
    public static final double POSITIVE_INFINITY = 1.0 / 0.0;

    // 负无穷
    public static final double NEGATIVE_INFINITY = -1.0 / 0.0;

    // 表示非数 (即不是Number)
    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;

    // 位数
    public static final int SIZE = 64;

    // 字节数,  jdk1.8新增属性
    public static final int BYTES = SIZE / Byte.SIZE;

    // Double类型class实例
    @SuppressWarnings("unchecked")
    public static final Class<Double>   TYPE = (Class<Double>) Class.getPrimitiveClass("double");

    //转换为String
    public static String toString(double d) {
        return FloatingDecimal.toJavaFormatString(d);
    } //静态
    public String toString() {
        return toString(value);
    }  //实例,调用静态方法
    /**
     * 返回 double 参数的十六进制字符串表示形式
     */
    public static String toHexString(double d) {

        if (!isFinite(d) )
            // For infinity and NaN, use the decimal output.
            return Double.toString(d);
        else {
            // Initialized to maximum size of output.
            StringBuilder answer = new StringBuilder(24);

            if (Math.copySign(1.0, d) == -1.0)    // value is negative,
                answer.append("-");                  // so append sign info
            answer.append("0x");
            d = Math.abs(d);
            if(d == 0.0) {
                answer.append("0.0p0");
            } else {
                boolean subnormal = (d < Double.MIN_NORMAL);
                // Isolate significand bits and OR in a high-order bit
                // so that the string representation has a known
                // length.
                long signifBits = (Double.doubleToLongBits(d)
                        & DoubleConsts.SIGNIF_BIT_MASK) |
                        0x1000000000000000L;
                // Subnormal values have a 0 implicit bit; normal
                // values have a 1 implicit bit.
                answer.append(subnormal ? "0." : "1.");
                // Isolate the low-order 13 digits of the hex
                // representation.  If all the digits are zero,
                // replace with a single 0; otherwise, remove all
                // trailing zeros.
                String signif = Long.toHexString(signifBits).substring(3,16);
                answer.append(signif.equals("0000000000000") ? // 13 zeros
                        "0":
                        signif.replaceFirst("0{1,12}$", ""));
                answer.append('p');
                // If the value is subnormal, use the E_min exponent
                // value for double; otherwise, extract and report d's
                // exponent (the representation of a subnormal uses
                // E_min -1).
                answer.append(subnormal ?
                        Double.MIN_EXPONENT:
                        Math.getExponent(d));
            }
            return answer.toString();
        }
    }

    //根据参数返回新创建的Double对象,推荐使用这种构造器
    public static Double valueOf(String s) throws NumberFormatException {
        return new Double(parseDouble(s));
    }

    public static Double valueOf(double d) {
        return new Double(d);
    }

    //根据String返回double基本类型值
    public static double parseDouble(String s) throws NumberFormatException {
        return FloatingDecimal.parseDouble(s);
    }

    //判断 double是否是无穷大 (正无穷或者负无穷)
    public static boolean isInfinite(double v) {
        return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
    }

    public boolean isInfinite() {
        return isInfinite(value);
    }

    //判断double是都是有限的
    public static boolean isFinite(double d) {
        return Math.abs(d) <= Double.MAX_VALUE;
    }

    // 此包装类的基本类型值
    private final double value;

    //构造器,jdk9被废弃,new这种方式在jdk9之后都不推荐使用了,改用valueOf形式API
    @Deprecated(since="9")
    public Double(double value) {
        this.value = value;
    }

    @Deprecated(since="9")
    public Double(String s) throws NumberFormatException {
        value = parseDouble(s);
    }

    //判断是否为NaN
    public boolean isNaN() {
        return isNaN(value);
    }

    public static boolean isNaN(double v) {
        return (v != v); //  NaN 与 NaN不相等
    }

    //基本类型转换
    public byte byteValue() {
        return (byte)value;
    }
    public short shortValue() {
        return (short)value;
    }
    public int intValue() {
        return (int)value;
    }
    public long longValue() {
        return (long)value;
    }
    public float floatValue() {
        return (float)value;
    }
    public double doubleValue() {
        return value;
    }

    @Override
    public int hashCode() {
        return Double.hashCode(value);
    }

    // jdk8新增方法
    public static int hashCode(double value) {
        long bits = doubleToLongBits(value);
        return (int)(bits ^ (bits >>> 32));
    }


    public boolean equals(Object obj) {
        return (obj instanceof Double)
                && (doubleToLongBits(((Double)obj).value) ==
                doubleToLongBits(value));
    }

    //double 转 long
    public static long doubleToLongBits(double value) {
        if (!isNaN(value)) {
            return doubleToRawLongBits(value);
        }
        return 0x7ff8000000000000L;  // NaN 固定为此值
    }


    public static native long doubleToRawLongBits(double value);

    public static native double longBitsToDouble(long bits);

    //比较大小
    public int compareTo(Double anotherDouble) {
        return Double.compare(value, anotherDouble.value);
    }

    //比较两个double的大小
    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)
    }

    // 求和, jdk8新增运算
    public static double sum(double a, double b) {
        return a + b;
    }

    //返回两个double中比较大的那个数,jdk8新增运算
    public static double max(double a, double b) {
        return Math.max(a, b);
    }

    //返回两个double中比较小的那个数,jdk8新增运算
    public static double min(double a, double b) {
        return Math.min(a, b);
    }

    /** use serialVersionUID from JDK 1.0.2 for interoperability */
    private static final long serialVersionUID = -9172774392245257468L;

四 Float类

Float和Double的源码基本上是一样的,这里不再多记录,可以参考Double的代码解析

posted @ 2020-01-14 13:50  侯小厨  阅读(564)  评论(0编辑  收藏  举报
Fork me on Gitee