Java基礎-Integer源碼

上班閑的時候看下源碼,邊看邊更新,歡迎評論

繼承關系

image.png

其中 Number 是個抽象類,主要抽象了一下方法:


image.png

即數(shù)值型的類型轉換

變量

@Native public static final int MIN_VALUE = 0x80000000;
int 型最小值,表示-2^(32-1)

@Native public static final int MAX_VALUE = 0x7fffffff;
int 型最大值,表示 2^(32-1) - 1

因為 Java 都是有符號的數(shù)值,所以 int 范圍是-2^(32 - 1)~2^(32-1)-1

public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int"); 表示原始類型 int 的類型

將數(shù)字表示為字符串的所有可能字符
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'
    };
這兩個數(shù)組是用來進行數(shù)值轉字符串時使用的,后面會講
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',
        } ;
這個是用來計算數(shù)值長度的
final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
                                      99999999, 999999999, Integer.MAX_VALUE };

private final int value; 數(shù)值的實際存放

@Native public static final int SIZE = 32; int 型有 32 位

public static final int BYTES = SIZE / Byte.SIZE; 8 個字節(jié)

函數(shù)

toString

static int stringSize(int x) {
        for (int i=0; ; i++)
// 上面有sizeTable這個數(shù)組,作用是用某個長度的最大值做界限,判斷數(shù)值是屬于哪個長度的。例如668,小于999,999的下標是2,得出668的長度是3
            if (x <= sizeTable[i])
                return i+1;
    }

  public static String toString(int i) {
        if (i == Integer.MIN_VALUE)
            return "-2147483648";
        int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
        char[] buf = new char[size];
        getChars(i, size, buf);
        return new String(buf, true);
    }
//將數(shù)值轉化為char數(shù)組
static void getChars(int i, int index, char[] buf) {

        int q, r;
        int charPos = index;
        char sign = 0;

        if (i < 0) {
            sign = '-';
            i = -i;
        }

        // Generate two digits per iteration
// 65536是2^16,這里先處理是為了下面的操作不會溢出
        while (i >= 65536) {
            q = i / 100;
        // really: r = i - (q * 100);
// 左移相當于乘2,所以:i- (q * 2^6 + q * 2^5 + q * 2 ^ 2)=i- q*100, 使用位移操作是因為jvm會對位移左右優(yōu)化
            r = i - ((q << 6) + (q << 5) + (q << 2));
            i = q;
// 獲取數(shù)值的個位數(shù)
            buf [--charPos] = DigitOnes[r];
// 獲取數(shù)值十位數(shù)
            buf [--charPos] = DigitTens[r];
        }

        // Fall thru to fast mode for smaller numbers
        // assert(i <= 65536, i);
        for (;;) {
// 為了保證這里的乘法不會溢出,所以要有上面的操作
// 這里是用位移和乘法代替除法,2^19=524288,所以下面的操作約等于 i/10
            q = (i * 52429) >>> (16+3);
//取個位數(shù)
            r = i - ((q << 3) + (q << 1));  // r = i-(q*10) ...
            buf [--charPos] = digits [r];
            i = q;
            if (i == 0) break;
        }
        if (sign != 0) {
            buf [--charPos] = sign;
        }
    }

作者:suruns
鏈接:http://pipe.suruns.com/blogs/suruns/articles/2019/10/11/1570758958967
來源:Pipe著作權歸作者所有。商業(yè)轉載請聯(lián)系作者獲得授權,非商業(yè)轉載請注明出處。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容