美文网首页
原始类型包装类解读(Float、Double)

原始类型包装类解读(Float、Double)

作者: sollian | 来源:发表于2018-06-21 18:16 被阅读30次

    Double和Float包含的方法大致一样,所以这里重点看Float,Double可以参照Float。


    Float继承自Number,仅有一个final float类型的成员变量value,用来保存对应的原始类型。

    静态常量

    //正无穷大,ps:谁说0不能做除数的😏
    public static final float POSITIVE_INFINITY = 1.0f / 0.0f;
    //负无穷大
    public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;
    //NaN(Not a Number)
    public static final float NaN = 0.0f / 0.0f;
    public static final float MAX_VALUE = 0x1.fffffeP+127f;
    public static final float MIN_NORMAL = 0x1.0p-126f;
    public static final float MIN_VALUE = 0x0.000002P-126f;
    //占用32bit
    public static final int SIZE = 32;
    public static final int BYTES = SIZE / Byte.SIZE;
    public static final Class<Float> TYPE = (Class<Float>) float[].class.getComponentType();
    

    构造函数

        public Float(float value) {
            this.value = value;
        }
    
        public Float(double value) {
            this.value = (float)value;
        }
    
        public Float(String s) throws NumberFormatException {
            value = parseFloat(s);
        }
    

    public static native float intBitsToFloat(int bits)

    将整型参数对应的32位bit值转换为对应的浮点数。
    如:

    • 0x7f800000对应POSITIVE_INFINITY
    • 0xff800000对应NEGATIVE_INFINITY
    • 0x7fc00000对应NaN
    • 0x7f7fffff对应MAX_VALUE
    • 0x1对应MIN_VALUE

    其他方法

        public boolean isNaN() {
            return isNaN(value);
        }
    
        public boolean isInfinite() {
            return isInfinite(value);
        }
    
        public static boolean isNaN(float v) {
            return (v != v);
        }
    
        public static boolean isInfinite(float v) {
            return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
        }
    
        public static Float valueOf(float f) {
            return new Float(f);
        }
    
        public static Float valueOf(String s) throws NumberFormatException {
            return new Float(parseFloat(s));
        }
    
        public static float parseFloat(String s) throws NumberFormatException {
            return FloatingDecimal.parseFloat(s);
        }
    
        //是否是有限的
        public static boolean isFinite(float f) {
           return Math.abs(f) <= FloatConsts.MAX_VALUE;
        }
    

    Double

    静态常量

    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;
    public static final double MIN_NORMAL = 0x1.0p-1022;
    public static final double MIN_VALUE = 0x0.0000000000001P-1022;
    public static final int SIZE = 64;
    public static final int BYTES = SIZE / Byte.SIZE;
    public static final Class<Double> TYPE = (Class<Double>) double[].class.getComponentType();
    

    相关文章

      网友评论

          本文标题:原始类型包装类解读(Float、Double)

          本文链接:https://www.haomeiwen.com/subject/jvlcyftx.html