JDK1.8源码解读之Integer

217 阅读6分钟

概述

Float类在对象中包装了原始类型float的值。 Float类型的对象包含一个类型为float的字段。此外,此类还提供了几种将float转换为String以及将String转换为float的方法,以及处理常量时有用的其他常量和方法。

继承关系

public final class Integer extends Number implements Comparable

继承自 Number 实现了 Comparable 接口

内部类
IntegerCache

创建和存储了常用的int值对应的Integer对象。在需要[-128,127]范围内的Integer对象时,直接用已经创建的。节省了重新创建的开销。

private static class IntegerCache {
    static final int low = -128;
    static final int high;
    static final Integer cache[];

    static {
        // high value may be configured by property
        int h = 127;
        String integerCacheHighPropValue =
            sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
        if (integerCacheHighPropValue != null) {
            try {
                int i = parseInt(integerCacheHighPropValue);
                i = Math.max(i, 127);
                // Maximum array size is Integer.MAX_VALUE
                h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
            } catch( NumberFormatException nfe) {
                // If the property cannot be parsed into an int, ignore it.
            }
        }
        high = h;

        cache = new Integer[(high - low) + 1];
        int j = low;
        for(int k = 0; k < cache.length; k++)
            cache[k] = new Integer(j++);

        // range [-128, 127] must be interned (JLS7 5.1.7)
        assert IntegerCache.high >= 127;
    }

    private IntegerCache() {}
}

成员属性

private final float value;

用来存储Float的值。

@Native public static final int MIN_VALUE = 0x80000000;

int能表示的最小值,-2147483648。

@Native public static final int MAX_VALUE = 0x7fffffff;

int能表示的最大值 2147483647。

public static final Class TYPE = (Class) Class.getPrimitiveClass("int");

返回Integer对应的基本数据类型名称 也就是返回字符串 “int”

digits

将所有可能代表数字的字符缓存下来。

final static char[] digits = {
    '0' , '1' , '2' , '3' , '4' , '5' ,
    '6' , '7' , '8' , '9' , 'a' , 'b' ,
    'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
    'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
    'o' , 'p' , 'q' , 'r' , 's' , 't' ,
    'u' , 'v' , 'w' , 'x' , 'y' , 'z'
};
DigitTens && DigitTens

数字转化为字符时用的,直接找出对应的字符。为了提高运算速度。

final static char [] DigitTens = {
'0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
'1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
'2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
'3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
'4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
'5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
'6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
'7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
'8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
'9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
} ;

final static char [] DigitOnes = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
} ;

构造器

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

关键方法

+ toUnsignedString()

转化为无符号的int,然后转化为String

public static String toUnsignedString(int i, int radix) {
    return Long.toUnsignedString(toUnsignedLong(i), radix);
}
+ toHexString()

转化为16进制

public static String toHexString(int i) {
    return toUnsignedString0(i, 4);
}

相应的 还有转化为八进制和二进制的。

public static String toOctalString(int i) {
    return toUnsignedString0(i, 3);
}
public static String toBinaryString(int i) {
    return toUnsignedString0(i, 1);
}
+ parseInt()

按照指定的进制转化为int

public static int parseInt(String s, int radix)                throws NumberFormatException

还有一个重载方法,默认是10进制。

public static String toUnsignedString(int i)

按照指定的进制,转化为无符号的int

public static int parseUnsignedInt(String s, int radix)
                throws NumberFormatException { ... }

按照默认的进制(10),转化为无符号的int

public static int parseUnsignedInt(String s) throws NumberFormatException { ... }
+ valueOf()

按照指定的进制,转化为Integer对象

public static Integer valueOf(String s, int radix) throws NumberFormatException { ... }

按照默认的进制(10),转化为Integer对象

public static Integer valueOf(String s) throws NumberFormatException {
    return Integer.valueOf(parseInt(s, 10));
}
+ byteValue()

转化为byte类型,

public byte byteValue() {
    return (byte)value;
}

除此之外还有其他类似方法: shortValue(),intValue(),longValue(),floatValue(),doubleValue()

+ equals()

类型相同,并且值相同,才会返回true

public boolean equals(Object obj) {
    if (obj instanceof Integer) {
        return value == ((Integer)obj).intValue();
    }
    return false;
}
+ compareTo()

和另一个Double值进行比较。 实际上是两个double,按照long读取出来,进行比较。其相对大小是相同的,但是按照long读取出来会计算更快。

public int compareTo(Float anotherFloat) {
    return Float.compare(value, anotherFloat.value);
}
+ getInteger()

返回系统属性对应的Integer,有两个重构方法。

public static Integer getInteger(String nm) {
    return getInteger(nm, null);
}
public static Integer getInteger(String nm, int val) {
    Integer result = getInteger(nm, null);
    return (result == null) ? Integer.valueOf(val) : result;
}
public static Integer getInteger(String nm, Integer val) {
    String v = null;
    try {
        v = System.getProperty(nm);
    } catch (IllegalArgumentException | NullPointerException e) {
    }
    if (v != null) {
        try {
            return Integer.decode(v);
        } catch (NumberFormatException e) {
        }
    }
    return val;
}
+ decode()

将字符串解码位Integer对象

public static Integer decode(String nm) throws NumberFormatException { ... }
+ compare()

比较两个Integer,前者较大,返回1;相等,返回0;前者较小 返回-1。

public static int compare(int f1, int f2) { ... }
+ compareTo()

将该Interge对象和另一个Integer对象比较,规则参照compare()

public int compareTo(Integer anotherInteger) {
    return compare(this.value, anotherInteger.value);
}
+ divideUnsigned()

除法运算

public static int divideUnsigned(int dividend, int divisor) {
    // In lieu of tricky code, for now just use long arithmetic.
    return (int)(toUnsignedLong(dividend) / toUnsignedLong(divisor));
}
+ remainderUnsigned()

求余

public static int remainderUnsigned(int dividend, int divisor) {
    // In lieu of tricky code, for now just use long arithmetic.
    return (int)(toUnsignedLong(dividend) % toUnsignedLong(divisor));
}
+ highestOneBit()

转化为2进制后,最高位代表的数字。 即返回满足条件: 2^n < i 最大的 2^n

public static int highestOneBit(int i) { ... }
+ lowestOneBit()

对应的二进制的最低一位,表示的int

public static int lowestOneBit(int i) {
    // HD, Section 2-1
    return i & -i;
}
+ rotateLeft() && + rotateRightrotateRight()

循环左移/循环右移

public static int rotateLeft(int i, int distance)
public static int rotateRight(int i, int distance)
reverse()

32位倒序,采用的是位移操作。

public static int reverse(int i) {
    // HD, Figure 7-1
    i = (i & 0x55555555) << 1 | (i >>> 1) & 0x55555555;
    i = (i & 0x33333333) << 2 | (i >>> 2) & 0x33333333;
    i = (i & 0x0f0f0f0f) << 4 | (i >>> 4) & 0x0f0f0f0f;
    i = (i << 24) | ((i & 0xff00) << 8) |
        ((i >>> 8) & 0xff00) | (i >>> 24);
    return i;
}
+ sum()

求和

public static int sum(int a, int b) {
    return a + b;
}
+ min()

求最小值

public static float min(float a, float b) {
    return Math.min(a, b);
}
+ max()

求最大值

public static float max(float a, float b) {
    return Math.max(a, b);
}

要点分析

DigitTens && DigitOnes

在转化的时候,存储了一些预定义的符号。在计算过程中就不需要重新计算这些变量了。

IntegerCache

内部类,定义了小范围内的Integer对象。在需要的直接使用已经定义的。避免后续使用的时候再次定义。节省系统开销。

reverse操作

采用的是位移实现的,而且采用的是归并的思路。 首先2个一组倒序位移。 然后4个一组倒序位移。 然后8个一组倒序位移。共4组。 然后这4组倒序位移。 采用位移操作计算速度更快。

希望和大家多多交流


我16年毕业以后,做的是前端,目前打算深入学习java开发。内容有任何问题,欢迎各位小伙伴们指正,也希望小伙伴们给我点赞和关注,给我留言,一起交流讨论,共同进步。