So las ich String-Klasse, als ich auf einem verwirrenden Konstruktor stolperte. Der Code geht soEmpty Konstruktor in String-Klasse
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
}
Ich verstehe nicht, was
"".value;
zu tun hat. Was ist das ""
? Ist es ein neues String object
? Wenn ja, mit welchem Konstruktor?
dies bedeutet String mit 0 Länge – emotionlessbananas
Dies ist einfach String-Literal-Objekt befindet sich im String-Pool .. Siehe http://StackOverflow.com/Questions/3052442/what-is-the-Differenz-between-text-and- new-stringtext –
Können Sie einen Link zu dem betreffenden Code angeben? Weil OpenJDK [7] (http://hg.openjdk.java.net/jdk7/jdk7/jdk/datei/9b8c96f96a0f/src/teilen/klassen/java/lang/String.java # l151) und [8] (http://hg.openjdk.java.net/jdk8/jdk8/jdk /file/687fd7c7986d/src/share/classes/java/lang/String.java#l138) habe 'this.value = new char [0];'. – dhke