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 < || 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 >> ;
} @HotSpotIntrinsicCandidate
// intrinsic performs no bounds checks
static char getChar(byte[] val, int index) {
// 判断是否越界
assert index >= && index < length(val) : "Trusted caller missed bounds check";
// 字符所在的字节启始位置
index <<= ;
// 按字节序还原字符
return (char)(((val[index++] & 0xff) << HI_BYTE_SHIFT) |
((val[index] & 0xff) << LO_BYTE_SHIFT));
}
2、
java String源码浅出的更多相关文章
- java Arrays源码浅出
1.toString 返回指定数组内容的字符串表示形式. demo: 由demo可窥见Arrays.toString的所做的工作就是将数组元素转换为字符串(以逗号分割数组元素,包裹在方括号中). 源码 ...
- 程序兵法:Java String 源码的排序算法(一)
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 这是泥瓦匠的第103篇原创 <程序兵法:Java Str ...
- java String源码学习
public final class String implements java.io.Serializable, Comparable<String>, CharSequence { ...
- Java String源码解析
public final class String implements java.io.Serializable, Comparable<String>, CharSequence { ...
- Java源码解析|String源码与常用方法
String源码与常用方法 1.栗子 代码: public class JavaStringClass { public static void main(String[] args) { Strin ...
- (转)Java中的String为什么是不可变的? -- String源码分析
背景:被问到很基础的知识点 string 自己答的很模糊 Java中的String为什么是不可变的? -- String源码分析 ps:最好去阅读原文 Java中的String为什么是不可变的 什 ...
- Java基础(八)--String(源码)、StringBuffer、StringBuilder
String源码:基于jdk1.8 public final class String implements Serializable, Comparable<String>, CharS ...
- Java集合源码分析(四)Vector<E>
Vector<E>简介 Vector也是基于数组实现的,是一个动态数组,其容量能自动增长. Vector是JDK1.0引入了,它的很多实现方法都加入了同步语句,因此是线程安全的(其实也只是 ...
- 编译哈工大语言技术平台云LTP(C++)源码及LTP4J(Java)源码
转自:编译哈工大语言技术平台云LTP(C++)源码及LTP4J(Java)源码 JDK:java version “1.8.0_31”Java(TM) SE Runtime Environment ( ...
随机推荐
- __proto__ VS. prototype in JavaScript
__proto__ VS. prototype in JavaScript http://dmitrysoshnikov.com/ecmascript/javascript-the-core/#a-p ...
- npm 错误记录
npm run dev iview-weapp@1.1.0 dev /Users/Jovins/Desktop/小程序/iview-weapp gulp --gulpfile build/build- ...
- JRE、JDK、JVM 及 JIT 之间有什么不同
java虚拟机(JVM) 使用java编程语言的主要优势就是平台的独立性.你曾经想知道过java怎么实现平台的独立性吗?对,就是虚拟机,它抽象化了硬件设备,开发者和他们的程序的得以操作系统.虚 ...
- 解决hibernate 序列化死循环的问题
用ie8时,请求json,eclipse直接死机!!!! 调试时,可以用chrome,看到无限循环的报错...类似 {"empty":true,"total": ...
- 阶段3 1.Mybatis_03.自定义Mybatis框架_6.自定义Mybatis的编码-实现基于XML的查询所有操作
接下来就可以写创建代理对象的方法了 类加载器,代理谁,就用谁的加载器,因为这里用daoInterfaceClass.getClassLoader() 第二个代理谁就要和谁有相同的接口,daoInter ...
- 性能测试工具之WebBench
一.简介 WebBench是一款在Linux下使用非常简单的压力测试工具.它的原理是:WebBench首先fork出多个子进程,每个子进程都循环做web访问测试.子进程把访问的结果通过pipe告诉父进 ...
- win7自带录像工具怎么打开?win7自带录像工具的使用方法
http://www.xitongcheng.com/jiaocheng/win7_article_28327.html 制作教程的好帮手 win7自带录像工具怎么打开?win7自带录像工具的使用方法 ...
- Java面试题全集(中)
这部分主要是与Java Web和Web Service相关的面试题. 96.阐述Servlet和CGI的区别? 答:Servlet与CGI的区别在于Servlet处于服务器进程中,它通过多线程方式运行 ...
- 虚拟机中Ubuntu安装及基本功能设置
虚拟机下安装ubuntu 虚拟机使用VMware14 PRO,在TOSHIBA EXT/Anon Comm Group\Experimental Environment\VMware下. 系统使用ub ...
- 跨 Appdomain 对象共享
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...