Java String transform to char array -
i want ask how "".value transform char array,thanks
public final class string implements java.io.serializable, comparable<string>, charsequence { /** value used character storage. */ private final char value[]; /** * initializes newly created {@code string} object represents * empty character sequence. note use of constructor * unnecessary since strings immutable. */ public string() { this.value = "".value; }
you should tell, jre implementation looking at, when cite source code.
however, code quite simple:
""refersstringconstant initialized jvm- since inside
string()constructor may called application code, not jvm internal initialization, may safely refer""constant like other
stringobject, hasvaluefield, insidestringconstructor, no problem accessprivatefield , copy reference; equivalent tostring tmp = ""; this.value = tmp.value;
since both, "" constant , instance created string() constructor represent empty strings, there no problem in sharing char[] array instance between them. however, there reasons against it:
- it optimizing uncommon case, there no reason ever use
string()constructor @ all - it fragile relies on particular jvm behavior, i.e. constant
""not constructed viastring()constructor; if assumption wrong, implementation create circular dependency
Comments
Post a Comment