java - Which String Object is accessed? -


as know when create new string object new keyword this:

string str = new string("new string have 2 objects"); 

it create 2 objects of, 1 on java heap memory , other on string pool.

so when call access "str" string object accessed(heap object or string pool object)?

according understanding string pool object accessed, if yes happens heap object?

if creating string object new

string str = new string("new string have 2 objects"); 

in such case, jvm create new string object in normal(non pool) heap memory , literal "new string have 2 objects" placed in string constant pool. variable str refer object in heap(non pool).

method ‘intern()’ usage

this best described java docs

when intern method invoked, if pool contains string equal string object determined equals(object) method, string pool returned. otherwise, string object added pool , reference string object returned.

string str = new string("new string have 2 objects");  str.intern(); 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -