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、

  • 相关阅读:
    链表10-开发可用链表(使用链表)
    LRU简单实现
    图片服务器搭建过程
    scp
    dubbo-admin无法访问
    linux tomcat启动后无法访问
    linux进程端口防火墙
    centos7 jdk
    com.alibaba.dubbo.rpc.RpcException: Fail to start server(url: dubbo://192.16。。
    linux与Unix切换到root用户
  • 原文地址:https://www.cnblogs.com/natian-ws/p/10251556.html
Copyright © 2011-2022 走看看