zoukankan      html  css  js  c++  java
  • 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、

  • 相关阅读:
    GCC学习笔记2
    $gcc help
    JVM证书制作步骤+耶鲁 CAS 配置
    oracle VARRAY与for .. loop
    docx转doc的方法
    java keytool 安全证书学习
    数字的格式化(简单几个,不过觉得很有用)
    人应该怎样的活着?!
    转;关于ORM
    终于可以回家了!
  • 原文地址:https://www.cnblogs.com/natian-ws/p/10251556.html
Copyright © 2011-2022 走看看