java-String源碼解讀(一)(由於個人基礎薄弱,未完)

本文只是對getChar()以及putChar()進行了解;
1 getChar:

@HotSpotIntrinsicCandidate
    // intrinsic performs no bounds checks
    static char getChar(byte[] val, int index) {
        assert index >= 0 && index < length(val) : "Trusted caller missed bounds check";
        index <<= 1;
        //0xff默認爲整型1111 1111b,0xff這裏先把val[index++]轉換成整型
        //左移8位=HI_BYTE_SHIFT,得到一個16進制的中間數1;
        //接着(val[index]&0xff)左移0位=LO_BYTE_SHIFT,得到中間數2
        //接着中間數1和中間數2進行或運算,得到一個16位的二進制數,然後(char)轉換
        return (char)(((val[index++] & 0xff) << HI_BYTE_SHIFT) |
                      ((val[index]   & 0xff) << LO_BYTE_SHIFT));
    }

2 putChar:

 @HotSpotIntrinsicCandidate
    // intrinsic performs no bounds checks
    static void putChar(byte[] val, int index, int c) {
        assert index >= 0 && index < length(val) : "Trusted caller missed bounds check";
        index <<= 1;
        //拿到當前32位0/1的c,然後右移8位,接着(byte)轉換獲取c字符的高8位,其實一共只用到了16位,32位的c有16位沒有用到,因爲java的字符有2個字節,共16位;
        val[index++] = (byte)(c >> HI_BYTE_SHIFT);
        //拿到當前32位0/1的c
        val[index]   = (byte)(c >> LO_BYTE_SHIFT);
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章