因此,當我偶然發現一個令人困惑的構造函數時,我正在閱讀String類。該代碼是這樣String類中的空構造函數
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
/** The value is used for character storage. */
private final char value[];
/** 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.
*/
public String() {
this.value = "".value;
}
// the rest of the class code
}
我不明白是什麼呢
"".value;
做。這是什麼""
?這是一個新的String object
?如果是,用什麼構造函數?
這意味着長度爲0的字符串 – emotionlessbananas
這很簡單字符串文字對象駐留在字符串池中..請參閱http://stackoverflow.com/questions/3052442/what-is-the-difference-between-text-and-新的字符串文字 –
你能提供一個鏈接到有問題的代碼嗎?因爲OpenJDK [7](http://hg.openjdk.java。net/jdk7/jdk7/jdk/file/9b8c96f96a0f/src/share/classes/java/lang/String.java#l151)和[8](http://hg.openjdk.java.net/jdk8/jdk8/jdk /file/687fd7c7986d/src/share/classes/java/lang/String.java#l138)有'this.value = new char [0];'。 – dhke