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 核心 ...
随机推荐
- gRPC编译教程
windows平台的编译 一.编译openssl ① 安装perl(可以使用ActivePerl),执行perl Configure VC-WIN64A no-asm .在这里解释一下参数含义,VC- ...
- SIGCHLD和wait/waipid函数的关系
SIGCHILD只是在子进程退出的时候发送给父进程的一个信号值,这是一种异步通知父进程的方式.父进程可以捕获,忽略这个信号,默认动作是忽略此信号. 常用的使用方式是,当SIGCHILD信号发生时候,主 ...
- PHP 模拟http 请求
php 模拟请求类 <?php /** * fangdasheng * http 模拟请求 */ class Myhttp { private $apiUrl; // 构造函数 public f ...
- wex5 sqllite本地数据库的运用
http://doc.wex5.com/?p=3774 需要引入包require("cordova!com.brodysoft.sqlitePlugin"); //本地数据库操作 ...
- RabbitMQ从安装到使用
一.在Linux中安装RabbitMQ 通过Docker安装: 获取镜像(选用management是带有管理界面的) docker pull rabbitmq:-management 查看下载好的镜像 ...
- ARIMA模型
ARIMA模型(英语:Autoregressive Integrated Moving Average model),差分整合移动平均自回归模型,又称整合移动平均自回归模型(移动也可称作滑动),时间序 ...
- linux上安装Eclipse
之所以要在linux上安装Eclipse,是因为一开始我是通过Eclipse+MingW+Samba+GDBserver方式在Windows上远程操作,准备编译调试nginx源代码的,可是在编译调试过 ...
- 005-使用smtp发送邮件报警
创建监控项: 如果有两个触发条件则中间用 and 连接,or等 此上 触发器已经创建好了,但是触发器的动作还需要去定义: 默认动作是停用的需要手动打开:
- mysql远程不能连接问题
解决Navicat无法连接到腾讯云MySQL的问题 工具/原料 Navicat phpstudy 腾讯云 Xshell6 方法/步骤 1 1.首先远程连接进入服务器,在cmd中输入mysql ...
- QByteArray与QString的互相转换
QByteArray baData; QString str = QString(baData); // 反过来转换: QByteArray by1 = str.toLatin1(); QByteAr ...