JDK源碼閱讀筆記--Integer

 class Integer extends Number
        implements Comparable<Integer>, Constable, ConstantDesc

接口Number中需要實現(xiàn)各種類型的轉(zhuǎn)換,比如longValue、floatValue等
其他接口與String的一樣。

字符串轉(zhuǎn)換

static int stringSize(int x) {
        int d = 1; //負(fù)數(shù)-標(biāo)志
        if (x >= 0) { //正數(shù)轉(zhuǎn)換位負(fù)數(shù)
            d = 0;
            x = -x;
        }
        int p = -10;
        //最大循環(huán)9次
        for (int i = 1; i < 10; i++) {
            if (x > p)
                return i + d;
            p = 10 * p;
        }
        return 10 + d;
    }

在轉(zhuǎn)換字符串,計算字符串長度時,對于正負(fù)數(shù)的處理時正數(shù)變?yōu)樨?fù)數(shù)再比較。因為負(fù)數(shù)的數(shù)值比較大2的31次方,正數(shù)時2的31次方-1,所以轉(zhuǎn)換為負(fù)數(shù)。

static int getChars(int i, int index, byte[] buf) {
        int q, r;
        int charPos = index;

        boolean negative = i < 0;
        if (!negative) {
            i = -i;
        }

        // Generate two digits per iteration
        while (i <= -100) {
            q = i / 100;
            r = (q * 100) - i;
            i = q;
            buf[--charPos] = DigitOnes[r];
            buf[--charPos] = DigitTens[r];
        }

        // We know there are at most two digits left at this point.
        q = i / 10;
        r = (q * 10) - i;
        buf[--charPos] = (byte)('0' + r);

        // Whatever left is the remaining digit.
        if (q < 0) {
            buf[--charPos] = (byte)('0' - q);
        }

        if (negative) {
            buf[--charPos] = (byte)'-';
        }
        return charPos;
    }

這里有一個while循環(huán),為了減少while循環(huán)的次數(shù),每次100取余后的結(jié)果到DigitOnes和DisgitTens數(shù)組里面直接獲取對應(yīng)的字符。例如個位字母

static final byte[] 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',
        } ;

緩存

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

        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                VM.getSavedProperty("java.lang.Integer.IntegerCache.high");

默認(rèn)緩存-128到127直接的值。也可以通過jvm啟動參數(shù)指定最大值。

@HotSpotIntrinsicCandidate
    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

可以看出緩存的作用。

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

相關(guān)閱讀更多精彩內(nèi)容

  • Integer類為java基本類型int的包裝類,除了前面提到的Byte類,Short類中的大部分方法,Integ...
    Kinsanity閱讀 952評論 0 2
  • 一、Python簡介和環(huán)境搭建以及pip的安裝 4課時實驗課主要內(nèi)容 【Python簡介】: Python 是一個...
    _小老虎_閱讀 6,319評論 0 10
  • 其他更多java基礎(chǔ)文章:java基礎(chǔ)學(xué)習(xí)(目錄) 轉(zhuǎn)載自 Java 源碼學(xué)習(xí)系列(三)——Integer學(xué)習(xí)的過...
    Hiwayz閱讀 777評論 0 0
  • ”世界上本沒有路,走的人多了,便成了路?!币部梢哉f世界上本沒有路,你愿意怎么走那變成了你的路。 條條大路通羅馬。
    成長訓(xùn)練營雨佳閱讀 318評論 0 0
  • 傷心難過,辭不達意。為什么一生孩子就各種被人嫌棄,好難過。
    忘苦草閱讀 299評論 0 0

友情鏈接更多精彩內(nèi)容