• java String源码浅出


    1、public char charAt(int index) 返回指定索引处的 char 值。

    源码:

        =====================String.class============================
        public char charAt(int index) {
            if (isLatin1()) { //Latin1单字节编码
                return StringLatin1.charAt(value, index);
            } else { // UTF-16双字节编码
                return StringUTF16.charAt(value, index);
            }
        }
        ==================StringLatin1.class=========================
        public static char charAt(byte[] value, int index) {
            if (index < 0 || index >= value.length) { // 判断是否越界
                throw new StringIndexOutOfBoundsException(index);
            }
            // 取出低8位并转为char类型
            return (char)(value[index] & 0xff);
        }
        ==================StringUTF16.class=========================
        public static char charAt(byte[] value, int index) {
            checkIndex(index, value);
            return getChar(value, index);
        }
        // UTF-16编码的字符串字符数,UTF-16双字节编码,字符数等于字节除以2
        public static int length(byte[] value) {
            return value.length >> 1;
        }
        
        @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;
            // 按字节序还原字符
            return (char)(((val[index++] & 0xff) << HI_BYTE_SHIFT) |
                          ((val[index]   & 0xff) << LO_BYTE_SHIFT));
        }
    
        
    View Code

    2、

  • 相关阅读:
    FreeBSD_11-系统管理——{Part_2-核心}
    FreeBSD_11-系统管理——{Part_1-xfce 桌面}
    FreeBSD_11-系统管理——{Part_0-基础}
    专题:initramfs & dracut
    拾遗:『Linux Capability』
    专题:Channel Bonding/bonding
    机器学习数学基础(三)
    洛谷P1879 [USACO06NOV]玉米田Corn Fields(状压dp)
    机器学习数学基础(二)
    莫队学习
  • 原文地址:https://www.cnblogs.com/natian-ws/p/10251556.html
Copyright © 2020-2023  润新知