1 String是不可變類
這句話其實大家都很熟悉了,那么具體什么是不可變類呢?一般認為:當對象一旦創建完成后,在正常情況下,對象的狀態不會因外界的改變而改變(對象的狀態是指對象的屬性,包括屬性的類型及屬性值)。
例如:
String s = "Google";
System.out.println("s = " + s);
s = "Runoob";
System.out.println("s = " + s);
輸出結果為:
s = Google
s = Runoob
從結果上看是改變了,但為什么門說String對象是不可變的呢?
原因在于實例中的 s只是指向堆內存中的引用,存儲的是對象在堆中的地址,而非對象本身,s本身存儲在棧內存中
當執行 s = "Runoob"; 創建了一個新的對象 "Runoob",而原來的 "Google" 還存在于內存中。
那么為什么String類具有不可變性呢,顯然,既然不可變說明String類中肯定沒有提供對外可setters方法。接下來來具體看一下String類的定義。
下面是String類中主要屬性的定義(Java 1.7源碼):
public final class String implements java.io.Serializable, Comparable<String>, CharSequence{
/** The value is used for character storage. */
private final char value[];
/** Cache the hash code for the string */
private int hash; // Default to 0
}
與之前版本的Java String源碼相比,String類減少了int offset 和 int count的定義。這樣變化的結果主要體現在:
- 避免之前版本的String對象subString時可能引起的內存泄露問題;
- 新版本的subString時間復雜度將有O(1)變為O(n);
通過上面String類的定義,類名前面用了final class修飾,因此,String類不能被繼承。對于其屬性定義,可以看出,屬性value[]和hash都是被定義成private類型,且由于沒有提供對外的public setters方法,String類屬性不可被改變。
其中,需要重點關注屬性value[],其被final char修飾,因此字符型數組value只會被賦值一次就不可修改。其存儲內容正好是String中的單個字符內容。
2 常量池
JVM為了提高性能和減少內存開銷,內部維護了一個字符串常量池,每當創建字符串常量時,JVM首先檢查字符串常量池,如果常量池中已經存在,則返回池中的字符串對象引用,否則創建該字符串對象并放入池中。
因此下述結果返回true。
String a = "abc";
String b = "abc";
System.out.print(a == b); //true
但與創建字符串常量方式不同的是,當使用new String(String str)方式等創建字符串對象時,不管字符串常量池中是否有與此相同內容的字符串,都會在堆內存中創建新的字符串對象。
因此,下面代碼片段有如下結果。
String a = "Hello";
String b = new String("Hello");
System.out.println(a == b); //false
System.out.println(a.equals(b)); //true
即使字符串內容相同,字符串常量池中的字符串與通過new String(..)等方式創建的字符串對象之間沒有直接的關系,但是,可以通過字符串的intern()方法找到此種關聯。
intern()方法返回字符串對象在字符串常量池中的對象引用,若字符串常量池中尚未有此字符串,則創建一新的字符串常量放置于池中。
于是,很據如上理解,很自然的,可以得到如下結果。
String a = "Hello";
System.out.println(a == a.intern()); //true
String b = new String("corn");
String c = b.intern();
System.out.println(b == c); //false
String d = "corn";
System.out.println(c == d); //true
3 String相關 +
String中使用 + 字符串連接符進行字符串連接時,連接操作最開始時如果都是字符串常量,編譯后將盡可能多的直接將字符串常量連接起來,形成新的字符串常量參與后續連接(通過反編譯工具jd-gui也可以方便的直接看出);
接下來的字符串連接是從左向右依次進行,對于不同的字符串,首先以最左邊的字符串為參數創建StringBuilder對象,然后依次對右邊進行append操作,最后將StringBuilder對象通過toString()方法轉換成String對象(注意:中間的多個字符串常量不會自動拼接)。
也就是說
String c = "xx" + "yy " + a + "zz" + "mm" + b;
實質上的實現過程是:
String c = new StringBuilder("xxyy").append(a).append("zz")
.append("mm").append(b).toString();
由于得出結論:當使用+進行多個字符串連接時,實際上是產生了一個StringBuilder對象和一個String對象。
4 String/StringBuilder/StringBuffer區別
String是不可變字符串對象,StringBuilder和StringBuffer是可變字符串對象(其內部的字符數組長度可變),StringBuffer線程安全,StringBuilder非線程安全。
5 既然String是不可變字符串對象,如何才能改變讓其可變?
既然String對象中沒有對外提供可用的public setters等方法,因此只能通過Java中的反射機制實現。因此,前文中說到的String是不可變字符串對象只是針對"正常情況下"。而非必然。
public static void stringReflection() throws Exception {
String s = "Hello World";
System.out.println("s = " + s); //Hello World
//獲取String類中的value字段
Field valueField = String.class.getDeclaredField("value");
//改變value屬性的訪問權限
valueField.setAccessible(true);
char[] value = (char[]) valueField.get(s);
//改變value所引用的數組中的第5個字符
value[5] = '_';
System.out.println("s = " + s); //Hello_World
}
6 String類常用方法
- 求字符串長度
public int length()//返回該字符串的長度
String str = new String("asdfzxc");
int strlength = str.length();//strlength = 7
- 求字符串某一位置字符
public char charAt(int index)//返回字符串中指定位置的字符;注意字符串中第一個字符索引是0,最后一個是length()-1。
String str = new String("asdfzxc");
char ch = str.charAt(4);//ch = z
- 提取子串
用String類的substring方法可以提取字符串中的子串,該方法有兩種常用參數:
1)public String substring(int beginIndex)//該方法從beginIndex位置起,從當前字符串中取出剩余的字符作為一個新的字符串返回。
2)public String substring(int beginIndex, int endIndex)//該方法從beginIndex位置起,從當前字符串中取出到endIndex-1位置的字符作為一個新的字符串返回。
String str1 = new String("asdfzxc");
String str2 = str1.substring(2);//str2 = "dfzxc"
String str3 = str1.substring(2,5);//str3 = "dfz"
- 字符串比較
1)public int compareTo(String anotherString)//該方法是對字符串內容按字典順序進行大小比較,通過返回的整數值指明當前字符串與參數字符串的大小關系。若當前對象比參數大則返回正整數,反之返回負整數,相等返回0。
2)public int compareToIgnore(String anotherString)//與compareTo方法相似,但忽略大小寫。
3)public boolean equals(Object anotherObject)//比較當前字符串和參數字符串,在兩個字符串相等的時候返回true,否則返回false。
4)public boolean equalsIgnoreCase(String anotherString)//與equals方法相似,但忽略大小寫。
String str1 = new String("abc");
String str2 = new String("ABC");
int a = str1.compareTo(str2);//a>0
int b = str1.compareTo(str2);//b=0
boolean c = str1.equals(str2);//c=false
boolean d = str1.equalsIgnoreCase(str2);//d=true
- 字符串連接
public String concat(String str)//將參數中的字符串str連接到當前字符串的后面,效果等價于"+"。
String str = "aa".concat("bb").concat("cc");
相當于String str = "aa"+"bb"+"cc";
- 字符串中單個字符查找
1)public int indexOf(int ch/String str)//用于查找當前字符串中字符或子串,返回字符或子串在當前字符串中從左邊起首次出現的位置,若沒有出現則返回-1。
2)public int indexOf(int ch/String str, int fromIndex)//改方法與第一種類似,區別在于該方法從fromIndex位置向后查找。
3)public int lastIndexOf(int ch/String str)//該方法與第一種類似,區別在于該方法從字符串的末尾位置向前查找。
4)public int lastIndexOf(int ch/String str, int fromIndex)//該方法與第二種方法類似,區別于該方法從fromIndex位置向前查找。
String str = "I am a good student";
int a = str.indexOf('a');//a = 2
int b = str.indexOf("good");//b = 7
int c = str.indexOf("w",2);//c = -1
int d = str.lastIndexOf("a");//d = 5
int e = str.lastIndexOf("a",3);//e = 2
- 字符串中字符的大小寫轉換
1)public String toLowerCase()//返回將當前字符串中所有字符轉換成小寫后的新串
2)public String toUpperCase()//返回將當前字符串中所有字符轉換成大寫后的新串
String str = new String("asDF");
String str1 = str.toLowerCase();//str1 = "asdf"
String str2 = str.toUpperCase();//str2 = "ASDF"
- 字符串中字符的替換
1)public String replace(char oldChar, char newChar)//用字符newChar替換當前字符串中所有的oldChar字符,并返回一個新的字符串。
2)public String replaceFirst(String regex, String replacement)//該方法用字符replacement的內容替換當前字符串中遇到的第一個和字符串regex相匹配的子串,應將新的字符串返回。
3)public String replaceAll(String regex, String replacement)//該方法用字符replacement的內容替換當前字符串中遇到的所有和字符串regex相匹配的子串,應將新的字符串返回。
String str = "asdzxcasd";
String str1 = str.replace('a','g');//str1 = "gsdzxcgsd"
String str2 = str.replace("asd","fgh");//str2 = "fghzxcfgh"
String str3 = str.replaceFirst("asd","fgh");//str3 = "fghzxcasd"
String str4 = str.replaceAll("asd","fgh");//str4 = "fghzxcfgh"
9、其他類方法
1)String trim()//截去字符串兩端的空格,但對于中間的空格不處理。
String str = " a sd ";
String str1 = str.trim();
int a = str.length();//a = 6
int b = str1.length();//b = 4
2)boolean statWith(String prefix)或boolean endWith(String suffix)//用來比較當前字符串的起始字符或子字符串prefix和終止字符或子字符串suffix是否和當前字符串相同,重載方法中同時還可以指定比較的開始位置offset。
String str = "asdfgh";
boolean a = str.statWith("as");//a = true
boolean b = str.endWith("gh");//b = true
3)regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
- ignoreCase -- 如果為 true,則比較字符時忽略大小寫。
- toffset -- 此字符串中子區域的起始偏移量。
- other -- 字符串參數。
- ooffset -- 字符串參數中子區域的起始偏移量。
- len -- 要比較的字符數。
String Str1 = new String("www.runoob.com");
String Str2 = new String("runoob");
String Str3 = new String("RUNOOB");
String Str4 = new String("runaab");
String Str5 = new String("aaaoob");
System.out.println(Str1.regionMatches(4, Str2, 0, 5)); //true
System.out.println(Str1.regionMatches(4, Str3, 0, 5)); //false
System.out.println(Str1.regionMatches(true, 4, Str3, 0, 5)); //true
System.out.println(Str1.regionMatches(4, Str4, 0, 4)); //false
System.out.println(Str1.regionMatches(4, Str5, 2, 4)); //false
4)contains(String str)//判斷參數s是否被包含在字符串中,并返回一個布爾類型的值。
String str = "student";
str.contains("stu");//true
str.contains("ok");//false
5)String[] split(String str)//將str作為分隔符進行字符串分解,分解后的字字符串在字符串數組中返回。
String str = "asd!qwe|zxc#";
String[] str1 = str.split("!|#");
//str1[0] = "asd";str1[1] = "qwe";str1[2] = "zxc";
7 字符串與基本類型的轉換
- 字符串轉換為基本類型
java.lang包中有Byte、Short、Integer、Float、Double類的調用方法:
1)public static byte parseByte(String s)
2)public static short parseShort(String s)
3)public static short parseInt(String s)
4)public static long parseLong(String s)
5)public static float parseFloat(String s)
6)public static double parseDouble(String s)
例如:
int n = Integer.parseInt("12");
float f = Float.parseFloat("12.34");
double d = Double.parseDouble("1.124");
- 基本類型轉換為字符串類型
String類中提供了String valueOf()放法,用作基本類型轉換為字符串類型。
1)static String valueOf(char data[])
2)static String valueOf(char data[], int offset, int count)
3)static String valueOf(boolean b)
4)static String valueOf(char c)
5)static String valueOf(int i)
6)static String valueOf(long l)
7)static String valueOf(float f)
8)static String valueOf(double d)
例如:
String s1 = String.valueOf(12);
String s1 = String.valueOf(12.34);
- 進制轉換
使用Long類中的方法得到整數之間的各種進制轉換的方法:
Long.toBinaryString(long l)
Long.toOctalString(long l)
Long.toHexString(long l)
Long.toString(long l, int p)//p作為任意進制