標籤:generate index 選擇 style rate images toc UI cte
String、StringBuilder和StringBuffer理解
1>String
java.lang.String 類
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 /** use serialVersionUID from JDK 1.0.2 for interoperability */ private static final long serialVersionUID = -6849794470754667710L; /** * Class String is special cased within the Serialization Stream Protocol. * * A String instance is written initially into an ObjectOutputStream in the * following format: * <pre> * <code>TC_STRING</code> (utf String) * </pre> * The String is written by method <code>DataOutput.writeUTF</code>. * A new handle is generated to refer to all future references to the * string instance within the stream. */ private static final ObjectStreamField[] serialPersistentFields = new ObjectStreamField[0];
......
從以上JDK中String源碼可以看出,String類被final修飾,在java中被final修飾的類是不能夠被繼承的,而且String類中的成員變數都被修飾成final。
而且可以看出String是通過char數組來實現儲存字串的。
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); } public String concat(String str) { int otherLen = str.length(); 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); } public String replace(char oldChar, char newChar) { if (oldChar != newChar) { int len = value.length; int i = -1; char[] val = value; /* avoid getfield opcode */ while (++i < len) { if (val[i] == oldChar) { break; } } 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; }
以上是String類的部分成員方法,可以看出他們都不是在原有的String對象上操作的。只要對String對象進行改變,則就會建立新的String對象,對原有的對象並沒有改變。
注意:String str1="abc" 和 String str2=new String("abc");有什麼區別呢? 代碼測試為證:
public class stringTest { public static void main(String[] args) { // TODO Auto-generated method stub String str1 = "abc"; String str2 = new String("abc"); String str3 = new String("abc"); String str4 = "abc"; System.out.println(str1 == str2); System.out.println(str2 == str3); System.out.println(str1 == str4); }}
結果是:
所以:string str1="abc"; 這樣的語句是在編譯時間產生了字面常量和字元引用,然後在運行時蔣"abc"儲存放到了運行時的常量池中,當下一個語句執行的時候就會從運行時常量池中尋找 是否存在,如果存在則將他的引用指向了字面常量,如果沒有就新開闢一個空間存放字面常量,並將引用指向他。
有關new語句建立對象時,都是在堆中進行的,不會檢查對象是否存在,所以每一個new語句都會建立一個新的對象。
2>StringBuilder
StringBuilder對String的改變操作是對原有對象進行操作的。
3>StringBuffer (安全執行緒)
StringBuffer和StringBuilder是類似的,之不過StringBuffer下的方法被snychronized修飾,安全執行緒。
這三個類是各有利弊,應當根據不同的情況來進行選擇使用:
當字串相加操作或者改動較少的情況下,建議使用 String str="hello"這種形式;
當字串相加操作較多的情況下,建議使用StringBuilder,如果採用了多線程,則使用StringBuffer。
java中String、StringBuilder和StringBuffer理解