JDK源碼解讀(第四彈:Integer之Integer的基本屬性)

Integer類繼承了Number類,主要作用是對基本類型int進行了包裝,提供了一些處理int的方法。
像parseInt,valueOf,intValue這些方法平時用的非常多,每個人都感覺對Integer很熟,看似這個類的源碼好像沒什麼可看的。不過不看不知道,一看嚇一跳,接下來我們來慢慢分析。

看源碼之前,可以先看一下Integer的 API文檔,原文鏈接如下:
https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html
也可以看我的翻譯版,鏈接如下:
https://blog.csdn.net/lianjiww/article/details/82531336

Integer類源碼很長,即便把註釋全部去掉也有幾大百行,這裏就先不把源碼全部貼上來,我們一部分一部分的看。而且由於Integer類要研究的內容其實很多,所以會分好幾篇文章來講,本篇先看Integer的幾個基本屬性。

先看一下Integer類的幾個的屬性:

@Native public static final int   MIN_VALUE = 0x80000000;

@Native public static final int   MAX_VALUE = 0x7fffffff;

@SuppressWarnings("unchecked")
    public static final Class<Integer>  TYPE = (Class<Integer>) Class.getPrimitiveClass("int");

private final int value;

@Native public static final int SIZE = 32;

public static final int BYTES = SIZE / Byte.SIZE;

MIN_VALUE:這個靜態變量表示int能取的最小值,爲0x80000000,也就是-2的31次方。
MAX_VALUE:這個靜態變量表示int能取的最大值,爲0x7fffffff,也就是2的31次方減1。
TYPE:就是基本類型int的類型,把TYPE打印出來的話就是輸出int。
value:既然Integer是對int的包裝,那肯定會有一個int的屬性用來保存Integer表示的int值,就是這個value了。
SIZE:使用二進制補碼錶示一個int值的bit數。
BYTES:使用二進制補碼錶示一個int值的byte數。

接下來看到有幾個static的char數組:

    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'
    };

    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',
        } ;

    final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
                                      99999999, 999999999, Integer.MAX_VALUE };

這幾個數組看起來古怪,其實有妙用。

digits數組包含了用於表示數字的所有可能的字符。int支持從2進制到36進制的表示方式,所以光0-9還不夠,得再有a-z總共36個字符才能表示所有不同進制的數字。
DigitTens數組用於獲取0到99之間某個數的十位數,比如想要獲取28的十位數,那就通過這個數組DigitTens[28],也就是得到2。
DigitOnes數組用於獲取0到99之間某個數的個位數,比如想要獲取28的個位數,那就通過這個數組DigitOnes[28],也就是得到8。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章