JDK源码阅读(1.8) —— String
String类有两个私有的变量,字符数组value,和整型变量hash(默认为0)。
1 /** The value is used for character storage. */ 2 private final char value[]; 3 4 /** Cache the hash code for the string */ 5 private int hash; // Default to 0
String的构造器会对value进行赋值,有时也对hash进行赋值。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * Initializes a newly created {@code String} object so that it represents 3 * an empty character sequence. Note that use of this constructor is 4 * unnecessary since Strings are immutable. 5 */ 6 public String() { 7 this.value = "".value; 8 } 9 10 /** 11 * Initializes a newly created {@code String} object so that it represents 12 * the same sequence of characters as the argument; in other words, the 13 * newly created string is a copy of the argument string. Unless an 14 * explicit copy of {@code original} is needed, use of this constructor is 15 * unnecessary since Strings are immutable. 16 * 17 * @param original 18 * A {@code String} 19 */ 20 public String(String original) { 21 this.value = original.value; 22 this.hash = original.hash; 23 } 24 25 /** 26 * Allocates a new {@code String} so that it represents the sequence of 27 * characters currently contained in the character array argument. The 28 * contents of the character array are copied; subsequent modification of 29 * the character array does not affect the newly created string. 30 * 31 * @param value 32 * The initial value of the string 33 */ 34 public String(char value[]) { 35 this.value = Arrays.copyOf(value, value.length); 36 }
下面对一些常用方法进行分析。
length()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * Returns the length of this string. 3 * The length is equal to the number of <a href="Character.html#unicode">Unicode 4 * code units</a> in the string. 5 * 6 * @return the length of the sequence of characters represented by this 7 * object. 8 */ 9 public int length() { 10 return value.length; 11 }
length()通过value数组的长度表示字符串的长度。
isEmpty()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * Returns {@code true} if, and only if, {@link #length()} is {@code 0}. 3 * 4 * @return {@code true} if {@link #length()} is {@code 0}, otherwise 5 * {@code false} 6 * 7 * @since 1.6 8 */ 9 public boolean isEmpty() { 10 return value.length == 0; 11 }
通过value.length == 0,判断字符串是否为空
charAt(int index)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public char charAt(int index) { 2 if ((index < 0) || (index >= value.length)) { 3 throw new StringIndexOutOfBoundsException(index); 4 } 5 return value[index]; 6 }
通过数组的方式去获取字符串对应位置的字符,基0。
equals(Object anObject)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public boolean equals(Object anObject) { 2 if (this == anObject) { 3 return true; 4 } 5 if (anObject instanceof String) { 6 String anotherString = (String)anObject; 7 int n = value.length; 8 if (n == anotherString.value.length) { 9 char v1[] = value; 10 char v2[] = anotherString.value; 11 int i = 0; 12 while (n-- != 0) { 13 if (v1[i] != v2[i]) 14 return false; 15 i++; 16 } 17 return true; 18 } 19 } 20 return false; 21 }
equals(Object obj)重写了Object的equals(Object obj).会先比较引用(String属于引用数据类型,不是基本数据类型)。当引用不同时,会进行 instanceof 判断
(如 if( str instanceof String)),判断该对象是否是字符串或其子类的一个实例。如果是,再比较value数组长度,相同的话,通过while循环,逐位比较字符是否相同,相同返回true,反之返回false.
Java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例。instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例。
hashCode()、
indexOf(int ch)、substring(int beginIndex)、concat(String str)、
replace(char oldChar, char newChar) 、contains(CharSequence s)、