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:

  • "" refers string constant initialized jvm
  • since inside string() constructor may called application code, not jvm internal initialization, may safely refer "" constant
  • like other string object, has value field, inside string constructor, no problem access private field , copy reference; equivalent to

    string 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 via string() constructor; if assumption wrong, implementation create circular dependency

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -