JDK中String类的源码分析(一)
1、String类是final的,不允许被继承
/** The value is used for character storage. */
private final char value[]; /** Cache the hash code for the string */
private int hash; // Default to 0
String类的内部就是维护了一个char数组;
2、构造方法,只需要看两个接受char数组的构造方法
public String(char value[]) {
this.value = Arrays.copyOf(value, value.length);
}
public String(char value[], int offset, int count) {
if (offset < 0) {
throw new StringIndexOutOfBoundsException(offset);
}
if (count < 0) {
throw new StringIndexOutOfBoundsException(count);
}
// Note: offset or count might be near -1>>>1.
if (offset > value.length - count) {
throw new StringIndexOutOfBoundsException(offset + count);
}
this.value = Arrays.copyOfRange(value, offset, offset+count);
}
这两个构造方法都用到了,Arrays工具类的copyOf方法,在这两个方法里面都调用了System.arraycopy方法;
因为System.arraycopy是一个系统本地方法,所以这个方法的效率很高,所以在构造String的时候效率也很高;
3、常用的length,charAt方法
通过第一条,很容易知道,这两个方法,实际上就是在操作char数组
public int length() {
return value.length;
}
public boolean isEmpty() {
return value.length == 0;
}
public char charAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index];
}
4、getBytes方法
调用了StringCoding.encode方法,encode方法调用另外一个重载的方法
static byte[] encode(char[] ca, int off, int len) {
String csn = Charset.defaultCharset().name();
try {
// use charset name encode() variant which provides caching.
return encode(csn, ca, off, len);
} catch (UnsupportedEncodingException x) {
warnUnsupportedCharset(csn);
}
try {
return encode("ISO-8859-1", ca, off, len);
} catch (UnsupportedEncodingException x) {
// If this code is hit during VM initialization, MessageUtils is
// the only way we will be able to get any kind of error message.
MessageUtils.err("ISO-8859-1 charset not available: "
+ x.toString());
// If we can not find ISO-8859-1 (a required encoding) then things
// are seriously wrong with the installation.
System.exit(1);
return null;
}
}
得到一个字节数组,是由ISO-8859-1编码得到,当然也可以用其他编码
5、compareTo方法
因为String实现了Comparable接口,所、所以必须实现compareTo方法
public int compareTo(String anotherString) {
int len1 = value.length;
int len2 = anotherString.value.length;
int lim = Math.min(len1, len2);
char v1[] = value;
char v2[] = anotherString.value;
int k = 0;
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
k++;
}
return len1 - len2;
}
其实是遍历的比较两个数组
JDK中String类的源码分析(一)的更多相关文章
- JDK中String类的源码分析(二)
1.startsWith(String prefix, int toffset)方法 包括startsWith(*),endsWith(*)方法,都是调用上述一个方法 public boolean s ...
- String类的源码分析
之前面试的时候被问到有没有看过String类的源码,楼主当时就慌了,回来赶紧补一课. 1.构造器(构造方法) String类提供了很多不同的构造器,分别对应了不同的字符串初始化方法,此处从源码中摘录如 ...
- Netty中NioEventLoopGroup的创建源码分析
NioEventLoopGroup的无参构造: public NioEventLoopGroup() { this(0); } 调用了单参的构造: public NioEventLoopGroup(i ...
- RocketMQ中Broker的启动源码分析(一)
在RocketMQ中,使用BrokerStartup作为启动类,相较于NameServer的启动,Broker作为RocketMQ的核心可复杂得多 [RocketMQ中NameServer的启动源码分 ...
- RocketMQ中Broker的启动源码分析(二)
接着上一篇博客 [RocketMQ中Broker的启动源码分析(一)] 在完成准备工作后,调用start方法: public static BrokerController start(Broker ...
- RocketMQ中Broker的消息存储源码分析
Broker和前面分析过的NameServer类似,需要在Pipeline责任链上通过NettyServerHandler来处理消息 [RocketMQ中NameServer的启动源码分析] 实际上就 ...
- Springboot中mybatis执行逻辑源码分析
Springboot中mybatis执行逻辑源码分析 在上一篇springboot整合mybatis源码分析已经讲了我们的Mapper接口,userMapper是通过MapperProxy实现的一个动 ...
- RocketMQ中PullConsumer的启动源码分析
通过DefaultMQPullConsumer作为默认实现,这里的启动过程和Producer很相似,但相比复杂一些 [RocketMQ中Producer的启动源码分析] DefaultMQPullCo ...
- Spring-MongoDB 关键类的源码分析
本文分析的是 spring-data-mongodb-1.9.2.RELEASE.jar 和 mongodb-driver-core-3.2.2.jar. 一.UML Class Diagram 核心 ...
随机推荐
- windows 通过cmd命令行管理防火墙
(1)恢复初始防火墙设置 netsh advfirewall reset(2)关闭防火墙 netsh advfirewall set allprofiles state off(3)启用桌面防火墙 n ...
- 用Python制作动态二维码
参考来源链接:https://mp.weixin.qq.com/s/p-ptQZD6_bjUhmU822OPww 今天在最大的同性交友社区GitHub上发现了一个比较有意思的项目,无论你是什么操作系统 ...
- 优雅的用两种方式爬网络 txt 文件【雾
TXT 文件?? (笑 这里爬的是 74xsw (咱好像也不怎么逛的网站)的英雄再临 ... 请注意这并不是教程,只是贴个代码仅供参考而已[雾 这里 用的 getTXT 的方式有两种,一种是每个章节分 ...
- npm学习(六)之如何创建 Node.js 模块
如何创建 Node.js 模块 Node.js 模块是一种可以发布到 npm 的包.当你创建一个新模块时,创建 package.json 文件是第一步. 你可以使用 npm init 命令创建 pac ...
- --解决Lock wait timeout exceeded; try restarting transaction
--解决Lock wait timeout exceeded; try restarting transaction select * from information_schema.innodb_t ...
- vue中获取本地ip
一.目的 获取当前访问的ip地址 二.思路 通过使用搜狐的api获取访问ip 三.操作步骤 1.在index.html中添加 <script src="http://pv.sohu.c ...
- Delphi 7的特点
- 设备中LPC2368芯片个例参数问题导致故障的分析
最近公司的设备客户报告在终端客户那里出现了板卡加热不受控,出现了持续加热导致设备一些贵重部件损坏.由于历史上很多现场问题,板卡什么拆到别的地方搭复现平台,基本都是以失败告终,所以出差去现场分析. 过程 ...
- Django学习系列7:使用模板解决“不测试常量”规则,使用模板重构
之前写的lists/tests.py中的单元测试,要查找特定的HTML字符串,但这不是测试HTML的高效方法. 单元测试规则之一“不测试常量”,编写断言检测HTML字符串中是否有制定的字符串序列,不是 ...
- Django学习系列4:编写第一个简单的应用代码
首页视图编写 lists/tests.py from django.test import TestCasefrom django.urls import resolvefrom lists.view ...