java 源码分析1 -String
1. String的本质是一个 char数组,实现了CharSequence 接口,
/** The value is used for character storage. */
private final char value[];
2.substring 分析
public String substring(int beginIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
int subLen = value.length - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
}
a 算出最终的字符串长度 subLen,调用new String(value, beginIndex, subLen);
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);
}
public static char[] copyOfRange(char[] original, int from, int to) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
char[] copy = new char[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}
调用System.arraycopy 复杂一个新的字符串返回 ,from 源的开始下标,Math.min(original.length - from, newLength) 新的字符串长度
3.replace
public String replace(char oldChar, char newChar) {
if (oldChar != newChar) {
int len = value.length;
int i = -1;
char[] val = value; /* avoid getfield opcode */
//找到相等的那个字符的下标i
while (++i < len) {
if (val[i] == oldChar) {
break;
}
}
if (i < len) {
//构造一个新的字符串数组buf
char buf[] = new char[len];
//把相等下标之前的char值赋值
for (int j = 0; j < i; j++) {
buf[j] = val[j];
}
//下标i开始遍历
while (i < len) {
char c = val[i];
//如果相等,进行替换
buf[i] = (c == oldChar) ? newChar : c;
i++;
}
return new String(buf, true);
}
}
return this;
}
4.replaceAll 调用了正则表达式相关的类库
public String replaceAll(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}
5.equals方法
public boolean equals(Object anObject) {
//如果引用相等,返回true
if (this == anObject) {
return true;
}
//如果传入的对象为String类型
if (anObject instanceof String) {
String anotherString = (String) anObject;
int n = value.length;
// 遍历字符数组每个char,
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;
}
6 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;
}
7 一个内部比较器 Comparator
public static final Comparator<String> CASE_INSENSITIVE_ORDER
= new CaseInsensitiveComparator();
private static class CaseInsensitiveComparator
implements Comparator<String>, java.io.Serializable {
// use serialVersionUID from JDK 1.2.2 for interoperability
private static final long serialVersionUID = 8575799808933029326L; public int compare(String s1, String s2) {
int n1 = s1.length();
int n2 = s2.length();
int min = Math.min(n1, n2);
for (int i = 0; i < min; i++) {
char c1 = s1.charAt(i);
char c2 = s2.charAt(i);
if (c1 != c2) {
c1 = Character.toUpperCase(c1);
c2 = Character.toUpperCase(c2);
if (c1 != c2) {
c1 = Character.toLowerCase(c1);
c2 = Character.toLowerCase(c2);
if (c1 != c2) {
// No overflow because of numeric promotion
return c1 - c2;
}
}
}
}
return n1 - n2;
}
}
java 源码分析1 -String的更多相关文章
- Java源码分析 | CharSequence
本文基于 OracleJDK 11, HotSpot 虚拟机. CharSequence 定义 CharSequence 是 java.lang 包下的一个接口,是 char 值的可读序列, 即其本身 ...
- Java源码分析:关于 HashMap 1.8 的重大更新(转载)
http://blog.csdn.net/carson_ho/article/details/79373134 前言 HashMap 在 Java 和 Android 开发中非常常见 而HashMap ...
- 【JDK源码分析】String的存储区与不可变性
// ... literals are interned by the compiler // and thus refer to the same object String s1 = " ...
- 【JDK源码分析】String的存储区与不可变性(转)
// ... literals are interned by the compiler // and thus refer to the same object String s1 = " ...
- 【JDK源码分析】String的存储区与不可变 专题
<Think in Java>中说:“关系操作符生成的是一个boolean结果,它们计算的是操作数的值之间的关系”. "=="判断的是两个对象的内存地址是否一样,适用于 ...
- Java 源码刨析 - String
[String 是如何实现的?它有哪些重要的方法?] String 内部实际存储结构为 char 数组,源码如下: public final class String implements java. ...
- Java源码分析:Guava之不可变集合ImmutableMap的源码分析
一.案例场景 遇到过这样的场景,在定义一个static修饰的Map时,使用了大量的put()方法赋值,就类似这样-- public static final Map<String,String& ...
- JAVA源码分析-HashMap源码分析(二)
本文继续分析HashMap的源码.本文的重点是resize()方法和HashMap中其他的一些方法,希望各位提出宝贵的意见. 话不多说,咱们上源码. final Node<K,V>[] r ...
- Java源码分析之LinkedList
LinkedList与ArrayList正好相对,同样是List的实现类,都有增删改查等方法,但是实现方法跟后者有很大的区别. 先归纳一下LinkedList包含的API 1.构造函数: ①Linke ...
随机推荐
- 贪心 HDOJ 5355 Cake
好的,数据加强了,wa了 题目传送门 /* 题意:1到n分成m组,每组和相等 贪心:先判断明显不符合的情况,否则肯定有解(可能数据弱?).贪心的思路是按照当前的最大值来取 如果最大值大于所需要的数字, ...
- 图论 HDOJ 5348 MZL's endless loop
题目传送门 /* 题意:给一个n个点,m条边的无向图,要求给m条边定方向,使得每个定点的出入度之差的绝对值小于等于1. 输出任意一种结果 图论:一个图,必定存在偶数个奇度顶点.那么从一个奇度定点深搜, ...
- EditText(1)EditText的类型和回车键的行为
1,常见类型 <EditText android:id="@+id/email_address" android:layout_width="fill_parent ...
- DHTML_____document对象的方法
<html> <head> <meta charset="utf-8"> <title>document对象的方法</titl ...
- python批量下载图片
从数据库拿了一批图片地址,需要一张一张的把图片下载下来,自从有了python,想到能省事就琢磨如何省事. 代码如下: import urllib.requestf=open("E:\999\ ...
- {Python}安装第三方包(setup.py)
在github上下载了records文件到本地. 解压文件 cmd切换到文件setup.py的目录下 先执行 python setup.py build 再执行python setup.py inst ...
- connection failed to http://nssa-sensor3:11000/oozie/?user.name=oozie(<urlopen erroer Errno 111] Connection refused>)解决办法(图文详解)
不多说,直接上干货! 解决办法 Copy/Paste oozie.services property tag set from oozie-default.xml to oozie-site.xml. ...
- 改变默认选择前1000行,编辑前200行【MSSQL】
- Android开发笔记(1)——View
笔记链接:http://www.cnblogs.com/igoslly/p/6781592.html 一.View基础知识 IDE——Integrated Developme ...
- PAT1021Deepset Root
题意: 连通则输出最深点.第一步找某个点的最深的,然后从这个最深的点查找其他最深点,做并集. 不连通则输出连通图个数. #include<iostream> #include<cst ...