String类源码分析(JDK1.7)
以下学习根据JDK1.7String类源代码做注释
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
//String类是final的,也就是说String类不允许被继承,实现了Serializable接口(可以序列化和反序列化),Comparale(可以进行自定义的字符串比较) CharSequence(一个可读序列。此接口对许多不同种类的 char 序列提供统一的只读访问。StringBuilder 和StringBuffer也实现了这个接口)
private final char value[]; //用于存放string字符的数组 private int hash; //表示string字符的哈希值,默认为0
//默认构造方法,一般不用,因为String字符串是不可改变的
public String() {
this.value = new char[0];
}
//有参构造方法,使用已存在的一个字符串创建一个相同字符序列的字符串
public String(String original) {
this.value = original.value;
this.hash = original.hash;
}
//使用一个字符数组创建一个字符串
public String(char value[]) {
this.value = Arrays.copyOf(value, value.length);
//使用Arrays类复制字符数组并赋值给String类声明的value
}
//使用字符数组的一部分创建一个字符串对象 offset字符数组开始位置,count数组开始位置往后的长度
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);
}
//将字节数组从offset开始,长度为length并以chatsetName编码转换成字符串
public String(byte bytes[], int offset, int length, String charsetName)
throws UnsupportedEncodingException {
if (charsetName == null)
throw new NullPointerException("charsetName");
checkBounds(bytes, offset, length);
this.value = StringCoding.decode(charsetName, bytes, offset, length);
}
//返回字符串的长度
public int length() {
return value.length;
}
//判断字符串长度是否为0
public boolean isEmpty{
retutn value.length==0
}
//根据下标获取字符
public char charAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index];
}
//equals 比较的是值
public boolean equals(Object anObject) {
if (this == anObject) {//如果是同一个对象,返回true
return true;
}
if (anObject instanceof String) { //判断是否是string对象
String anotherString = (String) anObject;
int n = value.length;
if (n == anotherString.value.length) {//判断长度是否一致
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])//比较每个字符是否一样
return false;
i++;
}
return true;
}
}
return false;
}
//采用乘法hash算法,字符串相同hash值一定相同,hash值相同,不一定字符串相同
public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value;
for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];每一次的hash值乘31+该字符
}
hash = h;
}
return h;
}
String类源码分析(JDK1.7)的更多相关文章
- String 类源码分析
String 源码分析 String 类代表字符序列,Java 中所有的字符串字面量都作为此类的实例. String 对象是不可变的,它们的值在创建之后就不能改变,因此 String 是线程安全的. ...
- String类源码分析
1.String类注释说明 /** * The {@code String} class represents character strings. All * string literals in ...
- ArrayList源码分析--jdk1.8
ArrayList概述 1. ArrayList是可以动态扩容和动态删除冗余容量的索引序列,基于数组实现的集合. 2. ArrayList支持随机访问.克隆.序列化,元素有序且可以重复. 3. ...
- List 接口以及实现类和相关类源码分析
List 接口以及实现类和相关类源码分析 List接口分析 接口描述 用户可以对列表进行随机的读取(get),插入(add),删除(remove),修改(set),也可批量增加(addAll),删除( ...
- ReentrantLock源码分析--jdk1.8
JDK1.8 ArrayList源码分析--jdk1.8LinkedList源码分析--jdk1.8HashMap源码分析--jdk1.8AQS源码分析--jdk1.8ReentrantLock源码分 ...
- Java Properties类源码分析
一.Properties类介绍 java.util.Properties继承自java.util.Hashtable,从jdk1.1版本开始,Properties的实现基本上就没有什么大的变动.从ht ...
- Java并发编程笔记之Unsafe类和LockSupport类源码分析
一.Unsafe类的源码分析 JDK的rt.jar包中的Unsafe类提供了硬件级别的原子操作,Unsafe里面的方法都是native方法,通过使用JNI的方式来访问本地C++实现库. rt.jar ...
- Cocos2d-X3.0 刨根问底(六)----- 调度器Scheduler类源码分析
上一章,我们分析Node类的源码,在Node类里面耦合了一个 Scheduler 类的对象,这章我们就来剖析Cocos2d-x的调度器 Scheduler 类的源码,从源码中去了解它的实现与应用方法. ...
- java中List接口的实现类 ArrayList,LinkedList,Vector 的区别 list实现类源码分析
java面试中经常被问到list常用的类以及内部实现机制,平时开发也经常用到list集合类,因此做一个源码级别的分析和比较之间的差异. 首先看一下List接口的的继承关系: list接口继承Colle ...
随机推荐
- Linux shell 脚本攻略之统计文件的行数、单词数和字符数
摘自:<Linux shell 脚本攻略>
- Debian 7.4 中配置PHP环境
准备工作 导入密钥 wget http://www.dotdeb.org/dotdeb.gpg sudo apt-key add dotdeb.gpg 添加源 vi /etc/apt/sources. ...
- 开发技巧01——改变Toast显示位置
1.获得Toast对象——Toast toast = Toast.makeText(this, "Top Left!", Toast.LENGTH_SHORT); 2.Toast对 ...
- mybatis缓存创建过程
带着 上篇 的问题,再来看看mybatis的创建过程 1.从SqlSessionFactoryBuilder解析mybatis-config.xml开始 对文件流解析 XMLConfigBuilder ...
- LInux下socket编程学习笔记
1.socket套接字: socket起源于Unix,而Unix/Linux基本哲学之一就是“一切皆文件”,都可以用“打开open –> 读写write/read –> 关闭close”模 ...
- saltstack实战2--远程执行之目标(target)
target 就是目标的意思,你要在那台机器上执行此命令或此状态.或者说将此动作或者状态文件推送给谁来执行,让那个minion执行可以进行一些匹配 对于拥有大量机器的环境,如果单独一台台的执行指定mi ...
- Java Concurrency - Fork/Join Framework
Normally, when you implement a simple, concurrent Java application, you implement some Runnable obje ...
- Nginx - Configuration File Syntax
Configuration Directives The Nginx configuration file can be described as a list of directives organ ...
- Nginx - Rewrite Module
Initially, the purpose of this module (as the name suggests) is to perform URL rewriting. This mecha ...
- ibatis.net 多线程的调试
ibatis是一个挺不错的半自动orm框架,从java移植到c# 在ibatis中是支持多线程操作的,但是这几天的使用过程中发现就框架本身任然存在一些问题,可能会让你对多线程的使用并不是那么的顺利 在 ...