Reputation: 33
I want to ask how does the "".value
transform char array
,Thanks
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;
}
Upvotes: 2
Views: 122
Reputation: 298143
You should tell, which JRE implementation you are looking at, when you cite its source code.
However, the code is quite simple:
""
refers to a String
constant which is initialized by the JVMString()
constructor which may get called by application code, but not JVM internal initialization, it may safely refer to the ""
constantlike any other String
object, it has the value
field, so inside the String
constructor, it is no problem to access that private
field and copy the reference; it is equivalent to
String tmp = "";
this.value = tmp.value;
Since both, the ""
constant and the instance created with the String()
constructor represent empty strings, there is no problem in sharing the char[]
array instance between them. However, there are reasons against it:
String()
constructor at all""
is not constructed via the String()
constructor; if that assumption is wrong, this implementation will create a circular dependencyUpvotes: 4
Reputation: 2423
"".value
creates empty char[]
array, equivalent of this.value = new char[0];
Upvotes: 0