2w字 詳解 String,yyds
關(guān)注公眾號(hào)“Java后端技術(shù)全棧”
回復(fù)“000”獲取程序員必備電子書
前言
大家好,我是老田,今天給大家分享java基礎(chǔ)知識(shí)之String。
String類的重要性就不必說了,可以說是我們后端開發(fā)用的最多的類,所以,很有必要好好來聊聊它。
本文主要內(nèi)容如下:

String簡介
我們先來說說,java中八大數(shù)據(jù)類型,然后在說String。
八大基本數(shù)據(jù)類型
byte:8位,最大存儲(chǔ)數(shù)據(jù)量是255,存放的數(shù)據(jù)范圍是-128~127之間。
short:16位,最大數(shù)據(jù)存儲(chǔ)量是65536,數(shù)據(jù)范圍是-32768~32767之間。
int:32位,最大數(shù)據(jù)存儲(chǔ)容量是2的32次方減1,數(shù)據(jù)范圍是負(fù)的2的31次方到正的2的31次方減1。
long:64位,最大數(shù)據(jù)存儲(chǔ)容量是2的64次方減1,數(shù)據(jù)范圍為負(fù)的2的63次方到正的2的63次方減1。
float:32位,數(shù)據(jù)范圍在3.4e-45~1.4e38,直接賦值時(shí)必須在數(shù)字后加上f或F。
double:64位,數(shù)據(jù)范圍在4.9e-324~1.8e308,賦值時(shí)可以加d或D也可以不加。
boolean:只有true和false兩個(gè)取值。
char:16位,存儲(chǔ)Unicode碼,用單引號(hào)賦值。
除了這八大數(shù)據(jù)類型以外(八大數(shù)據(jù)類型也有與之對(duì)應(yīng)的封裝類型,我相信你是知道的),Java中還有一種比較特殊的類型:String,字面意義就是字符串。
String官方介紹
英文版

地址:https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html
看不懂嗎?沒事,我們可以借用翻譯工具,瀏覽器自帶的,更希望的是你能看懂原版英文。
String 存在于咱們安裝的JDK目錄下rt.ar包中,全路徑名為:java.lang.String。我們java代碼中String用來表示字符串,比如:
String str = "中國夢(mèng),我的夢(mèng)";
String name = "zhangsan";
暫時(shí)先知道這些就可以了。
String使用
定義類型
在日常開發(fā)中,使用String的地方太多了,尤其是用來定義變量、常量的類型,基本上只要你碼代碼,總是能見到它。
比如:用戶信息,用實(shí)體類User來表示。
public class User{
private Long id;
private String userName;
private String address;
private String password;
....
}
常用方法演示
String類有20多個(gè)方法,下面給出一個(gè)使用示例(這里演示大部分方法,剩下的可以自行去試試)。
//案例代碼,來源于網(wǎng)絡(luò)
public class StringDemo {
public static void main(String[] args) throws Exception {
String str1 = "Hello World";
String str2 = "Hello World";
String str3 = "hello world";
String str4 = " hello world ";
//返回字符串的長度
System.out.println("r1: " + str1.length());
//比較兩個(gè)字符串的大小compareTo(返回的是int),0相等,復(fù)數(shù)小于,正數(shù)大于
System.out.println("r2 : " + str1.compareTo(str2));
//比較兩個(gè)字符串的大小compareTo(返回的是int),0相等,復(fù)數(shù)小于,正數(shù)大于
System.out.println("r3 : " + str1.compareTo(str3));
//字符串比較compareToIgnoreCase,忽略大小寫。0相等,復(fù)數(shù)小于,正數(shù)大于
System.out.println("r4 : " + str1.compareToIgnoreCase(str3));
//字符串查找indexOf,返回的是找到的第一個(gè)的位置,沒找到返回-1。從0開始
System.out.println("r5 : " + str1.indexOf("o"));
//查找字符串最后一次出現(xiàn)的位置lastIndexOf
System.out.println("r6 : " + str1.lastIndexOf("o"));
//刪除字符串中的一個(gè)字符,字符串從0開始的 substring(a, b)
//返回指定起始位置(含)到結(jié)束位置(不含)之間的字符串
System.out.println("r7 : " + str1.substring(0, 5) + str1.substring(6));
//字符串替換,替換所有
System.out.println("r8 : " + str1.replace("o", "h"));
//字符串替換,替換所有
System.out.println("r9 : " + str1.replaceAll("o", "h"));
//字符串替換,替換第一個(gè)
System.out.println("r10 : " + str1.replaceFirst("o", "h"));
//字符串反轉(zhuǎn)
System.out.println("r11 : " + new StringBuffer(str1).reverse());
//字符串反轉(zhuǎn)
System.out.println("r11’: " + new StringBuilder(str1).reverse());
//字符串分割
String[] temp = str1.split("\\ ");
for (String str : temp) {
System.out.println("r12 : " + str);
}
//字符串轉(zhuǎn)大寫
System.out.println("r13 : " + str1.toUpperCase());
//字符串轉(zhuǎn)小寫
System.out.println("r14 : " + str1.toLowerCase());
//去掉首尾空格
System.out.println("r15 : " + str4.trim());
//是否包含,大小寫區(qū)分
System.out.println("r16 : " + str1.contains("World"));
//返回指定位置字符
System.out.println("r17 : " + str1.charAt(4));
//測試此字符串是否以指定的后綴結(jié)束
System.out.println("r18 : " + str1.endsWith("d"));
//測試此字符串是否以指定的前綴開始
System.out.println("r19 : " + str1.startsWith("H"));
//測試此字符串從指定索引開始的子字符串是否以指定前綴開始
System.out.println("r20 : " + str1.startsWith("ll", 2));
//將指定字符串連接到此字符串的結(jié)尾。等價(jià)于用“+”
System.out.println("r21 : " + str1.concat("haha"));
//比較字符串的內(nèi)容是否相同
System.out.println("r22 : " + str1.equals(str2));
//與equals方法類似,忽略大小寫
System.out.println("r23 : " + str1.equalsIgnoreCase(str2));
//判斷是否是空字符串
System.out.println("r24: " + str1.isEmpty());
}
}
我們開發(fā)中差不多也就是這么使用了,但是如果你僅僅是使用很牛了,貌似遇到面試照樣會(huì)掛。所以,學(xué)知識(shí),不能停留在使用層面,需要更深層次的學(xué)習(xí)。
下面我們就來深層次的學(xué)習(xí)String,希望大家?guī)е活w平常的心學(xué)習(xí),不要害怕什么,燈籠是張紙,捅破不值錢。
String核心部分源碼分析
備注:JDK版本為1.8+,因?yàn)镴DK9版本中和舊版本有細(xì)微差別。
String類源碼注釋
/**
* The {@code String} class represents character strings. All
* string literals in Java programs, such as {@code "abc"}, are
* implemented as instances of this class.
* 這個(gè)String類代表字符串。java編程中的所有字符串常量。
* 比如說:"abc"就是這個(gè)String類的實(shí)例
* <p>
* Strings are constant; their values cannot be changed after they
* are created.
* 字符串是常量,他們一旦被創(chuàng)建后,他們的值是不能被修改。(重點(diǎn))
* String buffers support mutable strings.
* String緩存池支持可變的字符串,
* Because String objects are immutable they can be shared. For example:
* 因?yàn)镾tring字符串不可變,但他們可以被共享。比如:
* <blockquote><pre>
* String str = "abc";
* </pre></blockquote><p>
* is equivalent to:
* <blockquote><pre>
* char data[] = {'a', 'b', 'c'};
* String str = new String(data);
* </pre></blockquote><p>
* Here are some more examples of how strings can be used:
* String使用案例
* System.out.println("abc");
* String cde = "cde";
* System.out.println("abc" + cde);
* String c = "abc".substring(2,3);
* String d = cde.substring(1, 2);
* <p>
* The class {@code String} includes methods for examining
* individual characters of the sequence, for comparing strings, for
* searching strings, for extracting substrings, and for creating a
* copy of a string with all characters translated to uppercase or to
* lowercase. Case mapping is based on the Unicode Standard version
* specified by the {@link java.lang.Character Character} class.
* 這個(gè)String類包含了一些測評(píng)單個(gè)字符序列的方法,比如字符串比較,查找字符串,
* 提取字符串,和拷貝一個(gè)字符串的大小寫副本。
* 大小寫映射的是基于Character類支持的Unicode的字符集標(biāo)準(zhǔn)版本。
* <p>
* The Java language provides special support for the string
* concatenation operator ( + ), and for conversion of
* other objects to strings.
* java語言提供了對(duì)字符串的特殊支持,如:可以通過"+"號(hào)來進(jìn)行字符串的拼接操作,
* 為其他類提供了與字符串轉(zhuǎn)換的操作
* String concatenation is implemented
* through the {@code StringBuilder}(or {@code StringBuffer})
* class and its {@code append} method.
* 字符串的+號(hào)拼接操作是通過StringBuilder或者StringBuffer類的append()方法
* 來實(shí)現(xiàn)的
* String conversions are implemented through the method
* {@code toString}, defined by {@code Object} and
* inherited by all classes in Java.
* 對(duì)象與字符串的轉(zhuǎn)換操作是通過所有類的父類Object中定義的toString()方法來實(shí)現(xiàn)的
* For additional information on
* string concatenation and conversion, see Gosling, Joy, and Steele,
* <i>The Java Language Specification</i>.
*
* <p> Unless otherwise noted, passing a <tt>null</tt> argument to a constructor
* or method in this class will cause a {@link NullPointerException} to be
* thrown.
* 除非有特殊說明,否則傳一個(gè)null給String的構(gòu)造方法或者put方法,會(huì)報(bào)空指針異常的
* <p>A {@code String} represents a string in the UTF-16 format
* in which <em>supplementary characters</em> are represented by <em>surrogate
* pairs</em> (see the section <a href="Character.html#unicode">Unicode
* Character Representations</a> in the {@code Character} class for
* more information).
* 一個(gè)String 對(duì)象代表了一個(gè)UTF-16編碼語法組成的字符串
* Index values refer to {@code char} code units, so a supplementary
* character uses two positions in a {@code String}.
* <p>The {@code String} class provides methods for dealing with
* Unicode code points (i.e., characters), in addition to those for
* dealing with Unicode code units (i.e., {@code char} values).
* 索引值指向字符碼單元,所以一個(gè)字符在一個(gè)字符串中使用兩個(gè)位置,
* String 類提供了一些方法區(qū)處理單個(gè)Unicode編碼,除了那些處理Unicode代碼單元。
* @since JDK1.0
*/
以上便是String類注釋的整個(gè)片段,后面剩下的就是作者、相關(guān)類、相關(guān)方法以及從JDK哪個(gè)版本開始有的。
String類定義
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
....
}
類圖

String類被final修飾,表示String不可以被繼承。下面我們來說說String實(shí)現(xiàn)三個(gè)接口有什么用處:
實(shí)現(xiàn)Serializable,可以被序列化 實(shí)現(xiàn)Comparable,可以用于比較大小(按順序比較單個(gè)字符的ASCII碼) 實(shí)現(xiàn)CharSequence,表示是一個(gè)有序字符的序列,(因?yàn)镾tring的本質(zhì)是一個(gè)char類型數(shù)組)
簡單介紹final
修飾類:類不可被繼承,也就是說,String類不可被繼承了
修飾方法:把方法鎖定,以訪任何繼承類修改它的涵義
修飾遍歷:初始化后不可更改
重要成員
/** The value is used for character storage. */
// 來用存儲(chǔ)String內(nèi)容的
private final char value[];
// 存儲(chǔ)字符串哈希值,默認(rèn)值為0
private int hash; // Default to 0
// 實(shí)現(xiàn)序列化的標(biāo)識(shí)
private static final long serialVersionUID = -6849794470754667710L;
char value[]被final修飾,說明value[]數(shù)組是不可變的。
構(gòu)造方法
/**
* Initializes a newly created {@code String} object so that it represents
* an empty character sequence. Note that use of this constructor is
* unnecessary since Strings are immutable.
* 初始化新創(chuàng)建的String對(duì)象,時(shí)期表示空字符串序列。
* 注意:這個(gè)構(gòu)造方法的用法是沒必要的,因?yàn)樽址遣豢勺兊?br> */
public String() {
this.value = "".value;
}
無參構(gòu)造方法中是將一個(gè)空字符串的value值賦給當(dāng)前value。
/**
* Initializes a newly created {@code String} object so that it represents
* the same sequence of characters as the argument; in other words, the
* newly created string is a copy of the argument string. Unless an
* explicit copy of {@code original} is needed, use of this constructor is
* unnecessary since Strings are immutable.
* 初始化創(chuàng)建的String對(duì)象,時(shí)期表示與參數(shù)相同的字符串序列。
* 換句話說:新創(chuàng)建的字符串是參數(shù)自粗糙的副本。
* 除非,如果需要original的顯示副本,否則也是沒有必要使用此構(gòu)造方法的
* 因?yàn)樽址遣豢勺兊?br> * @param original
* A {@code String}
*/
public String(String original) {
this.value = original.value;
this.hash = original.hash;
}
//案例: String str=new String("abc");
把original的value賦給當(dāng)前的value,并把original的hash賦給當(dāng)前的hash。
/**
* Allocates a new {@code String} so that it represents the sequence of
* characters currently contained in the character array argument. The
* contents of the character array are copied; subsequent modification of
* the character array does not affect the newly created string.
* 分配一個(gè)新的{@code String},以便它表示字符數(shù)組參數(shù)中當(dāng)前包含的字符。這個(gè)
* 復(fù)制字符數(shù)組的內(nèi)容;隨后修改字符數(shù)組不影響新創(chuàng)建的字符串。
* @param value
* The initial value of the string
*/
public String(char value[]) {
//注:將傳過來的char數(shù)組copy到value數(shù)組里
this.value = Arrays.copyOf(value, value.length);
}
//Arrays類中的copyOf方法
public static char[] copyOf(char[] original, int newLength) {
//創(chuàng)建一個(gè)新的char數(shù)組
char[] copy = new char[newLength];
//把original數(shù)組中內(nèi)容拷貝到新建的char數(shù)組中
System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
//返回新建的char數(shù)組
return copy;
}
使用Arrays類的copyOf方法,新建一個(gè)char數(shù)組,將original的內(nèi)容放到新建的char數(shù)組中。
然后,把新建的char數(shù)組賦給當(dāng)前的vlaue。
public String(StringBuffer buffer) {
synchronized(buffer) {
this.value = Arrays.copyOf(buffer.getValue(), buffer.length());
}
}
因?yàn)镾tringBuffer是線程安全類,所以,這里加了同步鎖,保證線程安全。
public String(StringBuilder builder) {
this.value = Arrays.copyOf(builder.getValue(), builder.length());
}
StringBuilder是非線程安全的,這里也就沒有做線程安全處理,其他內(nèi)容和前面一樣。
注:很多時(shí)候我們不會(huì)這么去構(gòu)造,因?yàn)镾tringBuilder跟StringBuffer有toString方法如果不考慮線程安全,優(yōu)先選擇StringBuilder
這里就講這么多構(gòu)造方法,其他很復(fù)雜,也基本不用,所以,了解這些就夠了。如果對(duì)其他感興趣的,可以自行去研究研究。
常用方法分析
前面的使用案例中,我們已經(jīng)對(duì)String的大部分方法進(jìn)行演示一波,這里我們就挑幾個(gè)相對(duì)重要的方法進(jìn)行深度解析。
hashCode方法
hashCode()方法是在Object類中定義的,String對(duì)其進(jìn)行了重寫。
public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value;
//hash算法,s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
//使用{@codeint}算法,其中{@codes[i]}是<i> i</i>字符串的第個(gè)字符,
//{@code n}是字符串,{@code^}表示指數(shù)運(yùn)算。
for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];
}
hash = h;
}
return h;
}
hashCode的一個(gè)具體實(shí)現(xiàn),由于java體系中每個(gè)對(duì)象都可以轉(zhuǎn)換成String,因此他們應(yīng)該都是通過這個(gè)hash來實(shí)現(xiàn)的
接著,我們看看equals()方法;
equals()方法
equals()方法也是Object類中定義的,String類對(duì)其進(jìn)行了重寫。
public boolean equals(Object anObject) {
//首先會(huì)判斷是否是同一個(gè)對(duì)象
if (this == anObject) {
return true;
}
//判斷是否為String類型
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
//長度是否相同
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
//逐個(gè)遍歷判斷是否相等
//從后往前單個(gè)字符判斷,如果有不相等,返回假
while (n-- != 0) {
//不相等,直接返回false
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
補(bǔ)充:==比較
==比較基本數(shù)據(jù)類型,比較的是值
==比較引用數(shù)據(jù)類型,比較的是地址值
substring()方法
substring方法在工作使用的也是相當(dāng)?shù)亩啵饔镁褪墙厝∫欢巫址?/p>
public String substring(int beginIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
int subLen = value.length - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
//如果beginIndex==0,返回的是當(dāng)前對(duì)象,
//否則這里是new的一個(gè)新對(duì)象,其實(shí)String中的很多函數(shù)都是這樣的操作
return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
}
intern()方法
intern()方法是native修飾的方法,表示該方法為本地方法。
/*
* When the intern method is invoked, if the pool already contains a
* string equal to this {@code String} object as determined by
* the {@link #equals(Object)} method, then the string from the pool is
* returned. Otherwise, this {@code String} object is added to the
* pool and a reference to this {@code String} object is returned.
*/
public native String intern();
方法注釋會(huì)有寫到,意思就是調(diào)用方法時(shí),如果常量池有當(dāng)前String的值,就返回這個(gè)值,沒有就加進(jìn)去,返回這個(gè)值的引用。
案例如下
public class StringDemo {
public static void main(String[] args) throws Exception {
String str1 = "a";
String str2 = "b";
String str3 = "ab";
String str4 = str1 + str2;
String str5 = new String("ab");
System.out.println(str5 == str3);//堆內(nèi)存比較字符串池
//intern如果常量池有當(dāng)前String的值,就返回這個(gè)值,沒有就加進(jìn)去,返回這個(gè)值的引用
System.out.println(str5.intern() == str3);//引用的是同一個(gè)字符串池里的
System.out.println(str5.intern() == str4);//變量相加給一個(gè)新值,所以str4引用的是個(gè)新的
System.out.println(str4 == str3);//變量相加給一個(gè)新值,所以str4引用的是個(gè)新的
}
}
運(yùn)行結(jié)果
false
true
false
false
length()方法
獲取字符串長度,實(shí)際上是獲取字符數(shù)組長度 ,源碼就非常簡單了,沒什么好說的。
public int length() {
return value.length;
}
isEmpty() 方法
判斷字符串是否為空,實(shí)際上是盼復(fù)字符數(shù)組長度是否為0 ,源碼也是非常簡單,沒什么好說的。
public boolean isEmpty() {
return value.length == 0;
}
charAt(int index) 方法
根據(jù)索引參數(shù)獲取字符 。
public char charAt(int index) {
//索引小于0或者索引大于字符數(shù)組長度,則拋出越界異常
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
//返回字符數(shù)組指定位置字符
return value[index];
}
getBytes()方法
獲取字符串的字節(jié)數(shù)組,按照系統(tǒng)默認(rèn)字符編碼將字符串解碼為字節(jié)數(shù)組 。
public byte[] getBytes() {
return StringCoding.encode(value, 0, value.length);
}
compareTo()方法
這個(gè)方法寫的很巧妙,先從0開始判斷字符大小。如果兩個(gè)對(duì)象能比較字符的地方比較完了還相等,就直接返回自身長度減被比較對(duì)象長度,如果兩個(gè)字符串長度相等,則返回的是0,巧妙地判斷了三種情況。
public int compareTo(String anotherString) {
//自身對(duì)象字符串長度len1
int len1 = value.length;
//被比較對(duì)象字符串長度len2
int len2 = anotherString.value.length;
//取兩個(gè)字符串長度的最小值lim
int lim = Math.min(len1, len2);
char v1[] = value;
char v2[] = anotherString.value;
int k = 0;
//從value的第一個(gè)字符開始到最小長度lim處為止,如果字符不相等,
//返回自身(對(duì)象不相等處字符-被比較對(duì)象不相等字符)
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
k++;
}
//如果前面都相等,則返回(自身長度-被比較對(duì)象長度)
return len1 - len2;
}
startsWith()方法
public boolean startsWith(String prefix, int toffset) {
char ta[] = value;
int to = toffset;
char pa[] = prefix.value;
int po = 0;
int pc = prefix.value.length;
// Note: toffset might be near -1>>>1.
//如果起始地址小于0或者(起始地址+所比較對(duì)象長度)大于自身對(duì)象長度,返回假
if ((toffset < 0) || (toffset > value.length - pc)) {
return false;
}
//從所比較對(duì)象的末尾開始比較
while (--pc >= 0) {
if (ta[to++] != pa[po++]) {
return false;
}
}
return true;
}
public boolean startsWith(String prefix) {
return startsWith(prefix, 0);
}
public boolean endsWith(String suffix) {
return startsWith(suffix, value.length - suffix.value.length);
}
起始比較和末尾比較都是比較經(jīng)常用得到的方法,例如:在判斷一個(gè)字符串是不是http協(xié)議的,或者初步判斷一個(gè)文件是不是mp3文件,都可以采用這個(gè)方法進(jìn)行比較。
concat()方法
public String concat(String str) {
int otherLen = str.length();
//如果被添加的字符串為空,返回對(duì)象本身
if (otherLen == 0) {
return this;
}
int len = value.length;
char buf[] = Arrays.copyOf(value, len + otherLen);
str.getChars(buf, len);
return new String(buf, true);
}
concat方法也是經(jīng)常用的方法之一,它先判斷被添加字符串是否為空來決定要不要?jiǎng)?chuàng)建新的對(duì)象。
replace()方法
public String replace(char oldChar, char newChar) {
//新舊值先對(duì)比
if (oldChar != newChar) {
int len = value.length;
int i = -1;
char[] val = value;
//找到舊值最開始出現(xiàn)的位置
while (++i < len) {
if (val[i] == oldChar) {
break;
}
}
//從那個(gè)位置開始,直到末尾,用新值代替出現(xiàn)的舊值
if (i < len) {
char buf[] = new char[len];
for (int j = 0; j < i; j++) {
buf[j] = val[j];
}
while (i < len) {
char c = val[i];
buf[i] = (c == oldChar) ? newChar : c;
i++;
}
return new String(buf, true);
}
}
return this;
}
這個(gè)方法也有討巧的地方,例如最開始先找出舊值出現(xiàn)的位置,這樣節(jié)省了一部分對(duì)比的時(shí)間。replace(String oldStr,String newStr)方法通過正則表達(dá)式來判斷。
trim()方法
public String trim() {
int len = value.length;
int st = 0;
char[] val = value; /* avoid getfield opcode */
//找到字符串前段沒有空格的位置
while ((st < len) && (val[st] <= ' ')) {
st++;
}
//找到字符串末尾沒有空格的位置
while ((st < len) && (val[len - 1] <= ' ')) {
len--;
}
//如果前后都沒有出現(xiàn)空格,返回字符串本身
return ((st > 0) || (len < value.length)) ? substring(st, len) : this;
}
trim方法就是將字符串中的空白字符串刪掉。
valueOf()方法
public static String valueOf(boolean b) {
//如果b為true就返回"true"否則返回"false"
return b ? "true" : "false";
}
public static String valueOf(char c) {
//創(chuàng)建data[]數(shù)組 并把c添加進(jìn)去
char data[] = {c};
//創(chuàng)建一個(gè)新的String對(duì)象并進(jìn)行返回
return new String(data, true);
}
public static String valueOf(int i) {
//調(diào)用Integer對(duì)象的toString()方法并進(jìn)行返回
return Integer.toString(i);
}
//Integer類中的toString(i)方法
public static String toString(int i) {
//是否為Integer最小數(shù),是直接返回
if (i == Integer.MIN_VALUE)
return "-2147483648";
//這個(gè)i有多少位
int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
//創(chuàng)建一個(gè)char數(shù)組
char[] buf = new char[size];
//把i內(nèi)容方法char數(shù)組中區(qū)
getChars(i, size, buf);
//返回一個(gè)String對(duì)象
return new String(buf, true);
}
split() 方法
public String[] split(String regex) {
return split(regex, 0);
}
//使用到了正則表達(dá)式
public String[] split(String regex, int limit) {
//....
//源碼有點(diǎn)多了,反正就是里面使用到了正則表達(dá)式,進(jìn)行切分
}
split() 方法用于把一個(gè)字符串分割成字符串?dāng)?shù)組,返回一個(gè)字符串?dāng)?shù)組返回的數(shù)組中的字串不包括 regex自身。可選的“limit”是一個(gè)整數(shù),第一個(gè)方法中默認(rèn)是0,允許各位指定要返回的最大數(shù)組的元素個(gè)數(shù)。
常見方法源碼分析就這么多了,下面我們?cè)倩仡櫟绞褂脠鼍爸衼恚绕涫敲嬖囍小?/p>
String在面試中常見問題
如何比較字符串相同?
在java中比較對(duì)象是否相同,通常有兩種方法:
==equals方法
注意==用于基本數(shù)據(jù)類型的比較和用于引用類型的比較的區(qū)別。
==比較基本數(shù)據(jù)類型,比較的是值
==比較引用數(shù)據(jù)類型,比較的是地址值
另外,String對(duì)equals方法進(jìn)行了重寫,所以比較字符串咱們還是要使用equals方法來比較。主要是String的equals方法里包含了==的判斷(請(qǐng)看前面源碼分析部分)。
案例
public class StringDemo {
public static void main(String[] args) {
String st1 = "abc";
String st2 = "abc";
System.out.println(st1 == st2);
System.out.println(st1.equals(st2));
}
}
輸出
true
true
String str=new String("abc");這行代碼創(chuàng)建了幾個(gè)對(duì)象?
看下面這段代碼:
String str1 = "abc"; // 在常量池中
String str2 = new String("abc"); // 在堆上
關(guān)于這段代碼,創(chuàng)建了幾個(gè)對(duì)象,網(wǎng)上答案有多重,1個(gè),2個(gè)還有3個(gè)的。下面我們就來聊聊到底是幾個(gè)?
首先,我們需要明確的是;不管是str1還是str2,他們都是String類型的變量,不是對(duì)象,平時(shí),可能我們會(huì)叫str2對(duì)象,那只是為了便于理解,本質(zhì)上來說str2、str1都不是對(duì)象。
其次,String str="abc";的時(shí)候,字符串“abc”會(huì)被存儲(chǔ)在字符串常量池中,只有1份,此時(shí)的賦值操作等于是創(chuàng)建0個(gè)或1個(gè)對(duì)象。如果常量池中已經(jīng)存在了“abc”,那么不會(huì)再創(chuàng)建對(duì)象,直接將引用賦值給str1;如果常量池中沒有“abc”,那么創(chuàng)建一個(gè)對(duì)象,并將引用賦值給str1。
那么,通過new String("abc");的形式又是如何呢?
答案是1個(gè)或2個(gè)。
當(dāng)JVM遇到上述代碼時(shí),會(huì)先檢索常量池中是否存在“abc”,如果不存在“abc”這個(gè)字符串,則會(huì)先在常量池中創(chuàng)建這個(gè)一個(gè)字符串。然后再執(zhí)行new操作,會(huì)在堆內(nèi)存中創(chuàng)建一個(gè)存儲(chǔ)“abc”的String對(duì)象,對(duì)象的引用賦值給str2。此過程創(chuàng)建了2個(gè)對(duì)象。
當(dāng)然,如果檢索常量池時(shí)發(fā)現(xiàn)已經(jīng)存在了對(duì)應(yīng)的字符串,那么只會(huì)在堆內(nèi)創(chuàng)建一個(gè)新的String對(duì)象,此過程只創(chuàng)建了1個(gè)對(duì)象。
最后,如果單獨(dú)問String str=new String("abc");創(chuàng)建了幾個(gè)對(duì)象,切記:常量池中是否存在"abc",存在,創(chuàng)建一個(gè)對(duì)象;不存在創(chuàng)建兩個(gè)對(duì)象。
String 和 StringBuilder、StringBuffer 的區(qū)別
線程安全性
String 中的對(duì)象是不可變的,也就可以理解為常量,線程安全。AbstractStringBuilder 是 StringBuilder 與 StringBuffer 的公共父類,定義了一些字符串的基本操作,如 expandCapacity、append、insert、indexOf 等公共方法。StringBuffer 對(duì)方法加了同步鎖或者對(duì)調(diào)用的方法加了同步鎖,所以是線程安全的。StringBuilder 并沒有對(duì)方法進(jìn)行加同步鎖,所以是非線程安全的。
性能
每次對(duì) String 類型進(jìn)行改變的時(shí)候,都會(huì)生成一個(gè)新的 String 對(duì)象,然后將 指針指向新的 String 對(duì)象。StringBuffer 每次都會(huì)對(duì) StringBuffer 對(duì)象本身進(jìn)行操作,而不是生成新的對(duì)象并改變對(duì)象引用。相同情況下使用 StringBuilder 相比使用 StringBuffer 僅能獲得 10%~15% 左右的性能提升,但卻要冒多線程不安全的風(fēng)險(xiǎn)。
對(duì)于三者使用的總結(jié):
操作少量的數(shù)據(jù) ,推薦使用 String單線程操作字符串緩沖區(qū)下操作大量數(shù)據(jù),推薦使用 StringBuilder多線程操作字符串緩沖區(qū)下操作大量數(shù)據(jù) ,推薦使用 StringBuffer
String 和 JVM有什么關(guān)系?
String 常見的創(chuàng)建方式有兩種,new String() 的方式和直接賦值的方式,直接賦值的方式會(huì)先去字符串常量池中查找是否已經(jīng)有此值,如果有則把引用地址直接指向此值,否則會(huì)先在常量池中創(chuàng)建,然后再把引用指向此值;而 new String() 的方式一定會(huì)先在堆上創(chuàng)建一個(gè)字符串對(duì)象,然后再去常量池中查詢此字符串的值是否已經(jīng)存在,如果不存在會(huì)先在常量池中創(chuàng)建此字符串,然后把引用的值指向此字符串。
JVM中的常量池

字面量—文本字符串,也就是我們舉例中的 public String s = " abc "; 中的 "abc"。
用 final 修飾的成員變量,包括靜態(tài)變量、實(shí)例變量和局部變量。
請(qǐng)看下面這段代碼:
String s1 = new String("Java");
String s2 = s1.intern();
String s3 = "Java";
System.out.println(s1 == s2); // false
System.out.println(s2 == s3); // true
它們?cè)?JVM 存儲(chǔ)的位置,如下圖所示:

注意:JDK 1.7 之后把永生代換成的元空間,把字符串常量池從方法區(qū)移到了 Java 堆上。
除此之外編譯器還會(huì)對(duì) String 字符串做一些優(yōu)化,例如以下代碼:
String s1 = "Ja" + "va";
String s2 = "Java";
System.out.println(s1 == s2);
雖然 s1 拼接了多個(gè)字符串,但對(duì)比的結(jié)果卻是 true,我們使用反編譯工具,看到的結(jié)果如下:
Compiled from "StringExample.java"
public class com.lagou.interview.StringExample {
public com.lagou.interview.StringExample();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 3: 0
public static void main(java.lang.String[]);
Code:
0: ldc #2 // String Java
2: astore_1
3: ldc #2 // String Java
5: astore_2
6: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
9: aload_1
10: aload_2
11: if_acmpne 18
14: iconst_1
15: goto 19
18: iconst_0
19: invokevirtual #4 // Method java/io/PrintStream.println:(Z)V
22: return
LineNumberTable:
line 5: 0
line 6: 3
line 7: 6
line 8: 22
}
從編譯代碼 #2 可以看出,代碼 "Ja"+"va" 被直接編譯成了 "Java" ,因此 s1==s2 的結(jié)果才是 true,這就是編譯器對(duì)字符串優(yōu)化的結(jié)果。
如何判斷兩個(gè)字符串中含有幾個(gè)相同字符
將字符串轉(zhuǎn)化成數(shù)組 HashMap 方法 字符串直接進(jìn)行比較 正則表達(dá)式 HashSet 方法
String有沒有長度限制?是多少?為什么?
下面先看看length方法源碼:
private final char value[];
public int length() {
return value.length;
}
length()方法返回的是int類型,那可以得知String類型的長度肯定不能超過Integer.MAX_VALUE的。
答:首先字符串的內(nèi)容是由一個(gè)字符數(shù)組 char[] 來存儲(chǔ)的,由于數(shù)組的長度及索引是整數(shù),且String類中返回字符串長度的方法length() 的返回值也是int ,所以通過查看java源碼中的類Integer我們可以看到Integer的最大范圍是2^31 -1,由于數(shù)組是從0開始的,所以**數(shù)組的最大長度可以使【0~2^31】**通過計(jì)算是大概4GB。
但是通過翻閱java虛擬機(jī)手冊(cè)對(duì)class文件格式的定義以及常量池中對(duì)String類型的結(jié)構(gòu)體定義我們可以知道對(duì)于索引定義了u2,就是無符號(hào)占2個(gè)字節(jié),2個(gè)字節(jié)可以表示的最大范圍是2^16 -1 = 65535。
但是由于JVM需要1個(gè)字節(jié)表示結(jié)束指令,所以這個(gè)范圍就為65534了。超出這個(gè)范圍在編譯時(shí)期是會(huì)報(bào)錯(cuò)的,但是運(yùn)行時(shí)拼接或者賦值的話范圍是在整形的最大范圍。
字符串對(duì)象能否用在switch表達(dá)式中?
從JDK7開始的話,我們就可以在switch條件表達(dá)式中使用字符串了,也就是說7之前的版本是不可以的。
switch (str.toLowerCase()) {
case "tian":
value = 1;
break;
case "jiang":
value = 2;
break;
}
說說String中intern方法
在JDK7之前的版本,調(diào)用這個(gè)方法的時(shí)候,會(huì)去常量池中查看是否已經(jīng)存在這個(gè)常量了,如果已經(jīng)存在,那么直接返回這個(gè)常量在常量池中的地址值,如果不存在,則在常量池中創(chuàng)建一個(gè),并返回其地址值。
但是在JDK7以及之后的版本中,常量池從perm區(qū)搬到了heap區(qū)。intern檢測到這個(gè)常量在常量池中不存在的時(shí)候,不會(huì)直接在常量池中創(chuàng)建該對(duì)象了,而是將堆中的這個(gè)對(duì)象的引用直接存到常量池中,減少內(nèi)存開銷。
下面的案例
public class InternTest {
public static void main(String[] args) {
String str1 = new String("hello") + new String("world");
str1.intern();
String str2 = "helloworld";
System.out.println(str1 == str2);//true
System.out.println(str1.intern() == str2);//true
}
}
好了,關(guān)于Stirng類的分享就到此,歡迎找我探討更多技術(shù)問題。
另外一篇關(guān)于String類的文章:美團(tuán)面試題:String s = new String("111")會(huì)創(chuàng)建幾個(gè)對(duì)象?
參考
http://prren.cn/vWv68 、http://prren.cn/7ICgG、http://dsblb.cn/ku78V、王磊《 Java 源碼剖析 34 講》
歡迎關(guān)注微信公眾號(hào):互聯(lián)網(wǎng)全棧架構(gòu),收取更多有價(jià)值的信息。
