String a = new String ( "abc") created several objects

String str = new String ( "abc"); immediately after this code is often the problem, and that is what this line of code to create a few String objects?

I believe we no stranger to this question, the answer is known, two.

Then we unfold from this question, review some JAVA knowledge related to creating String objects together.  

We can put this line of code above into String str, =, "abc" and the new String () to look at four parts. String str just defines a variable named str String type, so it does not create the object; = str variable is initialized, the reference (or call handler) assigned to it an object, apparently did not create the object ; now only new String ( "abc") a. Then, new String ( "abc") Why can be seen as "abc" and the new String () do?

We look at is we call the constructor of the String:  

public String (String original) {// other code ...} As we all know, we often create an instance (object) of a class of the following two methods:

First, using a new object is created. 

Second, call the newInstance method of class Class, create objects using the reflection mechanism.

What we call above using new String class constructor method that creates an object and its reference assigned to the variable str. At the same time we note that the constructor method is called to accept the argument is a String object that is the "abc". Thus, we have to create a String object to introduce another way discussion - contains text within quotation marks.

 

String this way is unique, and there is a big difference between it and the new way.  

String str="abc";  

There is no doubt that this line of code creates a String object.  

String a = "abc"; String b = "abc"; that here it?

The answer is a.  

String a = "ab" + "cd"; look at it here?

The answer is three.

Here, we need to introduce a review of the relevant knowledge of the string pool.  

It exists in a string pool JAVA virtual machine (JVM) in which to save a lot of String object, and can be shared, so it improves the efficiency. Because the String class is final, its value can not be changed once created, so we do not worry about String objects share and bring chaos program. String pool maintained by the String class, we can call the intern () method to access the string pool.  

We look back at the String a = "abc" ;, when this line of code is executed, JAVA virtual machine first in the string pool to find whether there is a value of "abc" of such an object, its judgment is based on return value String class equals (Object obj) method. If so, then no longer create a new object, direct returns a reference to an object that already exists; if not, create the object, and then add it to the string pool, and then returns a reference to it. Therefore, it is not difficult to understand why the previous three examples in the first two examples are the answer.

 

Use only created between use quotation marks containing the text of the String object "+" new objects created connection will be added to the string pool. For all contain new way new objects (including null) "+" in connection expression, it generates a new object will not be added to the string pool, which we will not repeat them. Therefore, we advocate everyone to create a String object in order to improve efficiency by way quotes containing text, in fact, that is what we often adopt the program of.

 

Stack (stack): major basic types stored (or called built-in type) reference (char, byte, short, int, long, float, double, boolean) and objects, data can be shared, only to speed register (Register), faster than the heap. 

Heap (heap): for storing objects

Guess you like

Origin www.cnblogs.com/Young111/p/11273851.html