方法论:
分析jdk11源码的lang包,了解lang包下的具体实现,分析其实现过程,感悟其设计思路,逐步形成查看源码的思维
第一天:分析lang包中String相关的接口和类
1.String类
实现的接口:
1.Serializable
2.Comparable
3.CharSequence
A CharSequence is a readable sequence of char values. This interface provides uniform, read-only access to many different kinds of char sequences.
一个字符顺序的接口
变量:
1.char[] value
说明java存储字符串的本质是以字符数组的存储
2.int hash
3.long serialVersionUID:Serializable接口规定的
4.ObjectStreamField:不知道,留作问题
构造器
1.String():空字符串""
2.String(String original):字符串
3.String(char[] value)
4.String(char value[],int offset,int count)
5.String(int[] codePoints,int offset,int count):字符点
6.废弃_Stirng(byte ascii[],int hibyte,int offset,int count):askii码转化为String
7.废弃_String(byte ascii[],int hibyte):askii转化为String
8.String(byte bytes[],int offset,int length, String charsetName):将字节流采用一定的编码格式转化为String
9.String(byte bytes[], String charsetName)
10.String(byte bytes[], Charset charset)
11.String(byte bytes[],int offset,int length)
12.String(byte bytes[])
13.String(StringBuffer buffer)
14.String(StringBuilder builder)
15.String(char[] value,boolean share)
方法:
1.int length():返回数组的长度
2.boolean isEmpty():数组长度为0
3.char charAt(int index):索引上的字符值
4.int codePointAt(int index):索引上的字符点
5.int codePointBefore(int index):索引上之前一个字符点
6.int codePointCount(int beginIndex,int endIndex):字符点个数
7.int offsetByCodePoints(int index,int codePointOffset):索引对应的字符点的偏移量
8.包级访问_getChars(char dst[],int dstBegin):将当前字符串复制到新的数组中
9.getChars(int srcBegin,int srcEnd,char dst[],int dstBegin):将当前字符串的自己复制到目标字符数组中
10.getBytes(int srcBegin,int srcEnd,byte dst[],int dstBegin):将字符串的自己复制到字节数组中
11.toGenericString():泛型描述的字符串
12.byte[] getBytes(String charsetName):以特地字符格式转化为byte数组
13.byte[] getBytes(Charset charset)
14.byte[] getBytes()
15.boolean equals(Object anObject)
16.boolean contentEquals(StringBuffer sb)
17.boolean contentEquals(CharSequence cs)
18.boolean equalsIgnoreCase(String anotherString)
19.int compareToIgnoreCase(String str)
20.boolean regionMatches(int toffset, String other,int ooffset,int len):是否匹配字符串
21.boolean startsWith(String prefix,int toffset):从偏移量的位置开始是是否已该字符串开头的
22.boolean startsWith(String prefix)
23.boolean endsWith(String suffix)
24.indexOf(int ch)
25.indexOf(int ch,int fromIndex)
26.int lastIndexOf(int ch,int fromIndex):从后往前,第一个是ch的字符的偏移量
27.indexOf(String str)
28.int indexOf(char[] source,int sourceOffset,int sourceCount,String target,int fromIndex)
29.int indexOf(char[] source,int sourceOffset,int sourceCount,char[] target,int targetOffset,int targetCount,
int fromIndex)
30.int lastIndexOf(String str)
31.static int lastIndexOf(char[] source,int sourceOffset,int sourceCount,String target,int fromIndex)
32.static int lastIndexOf(char[] source,int sourceOffset,int sourceCount,char[] target,int targetOffset,int targetCount,int fromIndex)
33.String substring(int beginIndex)
34.String substring(int beginIndex,int endIndex)
35.CharSequence subSequence(int beginIndex,int endIndex)
36.String concat(String str)
37.String replace(char oldChar,char newChar)
38.boolean matches(String regex)
39.boolean contains(CharSequence s)
40.String replaceFirst(String regex, String replacement);
41.String replaceAll(String regex, String replacement) ;
42.String replace(CharSequence target, CharSequence replacement)
43.String[] split(String regex,int limit);
44.String[] split(String regex,int limit)
45.String[] split(String regex)
46.String toLowerCase()
47.String toUpperCase(Locale locale)
48.String toUpperCase()
49.char[] toCharArray()
50.String format(String format, Object... args)
51.static String format(Locale l, String format, Object... args)
52.static String valueOf(char data[])
53.static String valueOf(char data[],int offset,int count)
54.static String copyValueOf(char data[],int offset,int count)
55.static String copyValueOf(char data[])
56.static String valueOf(boolean b)
57.static String valueOf(char c)
58.static String valueOf(int i)
59.static String valueOf(long l)
60.static String valueOf(float f)
61.static String valueOf(double d)
总结:String 类的本质是有序的字符数组,所有操作也都是基于字符数组进行操作.细节处能够体会到简洁代码,以及代码重构的一些思想.