Home » Java » Analysis of Java based String object creation revi...

Analysis of Java based String object creation review

The

java virtual machine is divided into a heap, a stack, and a method area..String is a final class. Once it is created, it cannot be changed. Its value is


1.Sample, sample = new, Sample ("ABC");


The

sample object name is simply assigned a reference and does not allocate a storage space for it. The reference is generally stored in the stack, and the ABC is a constant
A literal, compile time directly storing its literals, such literals are generally created in String to maintain the constant pool in the method area,.New (Sample) are usually stored in heap objects by new out of the heap will preserve the integrity of the information object. So strictly speaking only to create a an object. If you think the constant pool inside the'abc'is also an object, that is the creation of the 2 objects, but I think only stored in the heap is the real object


2.String a = 123;


String B = "123"


System.out,.Println (a==b); //true


a and B are stored in String in the constant pool, after the creation of JVM first to find the string in the pool, to see if there is already a value of "123" object, if present, will no longer create new objects, direct reference to the returned object already exists; if it does not exist, then create the object then, add it to the string in the pool, then it will return. So when he founded B directly back to the "123" refers to a.


3.String a = new String ("123"); / / a reference points to heap objects in


String B = 123; / / to a constant pool


System.out,.Println (a==b); //false, of course, cannot equal


4 String a = "1" + "2" + "3"; create a few objects. "1." "2", "12", "3", "123", only 5 constant, according to the analysis of literal values stored in the constant pool inside, if the above 3 example analysis of the establishment, strictly speaking is no object, or is the creation of 5 objects










Latest