The reason why String s=new String(abc) created 2 objects

Question: String str=new String(“abc”); How many String objects are created by this line of code?

I believe that everyone is no stranger to this question, and the answer is well known, 2.

Next, let’s start from this question and review some JAVA knowledge related to creating String objects.

one sentence description

The first creation is because the passed parameter “abc” will create an object, which is equivalent to String param=”abc”,

The second creation is because new String() is used to create a string. At this time, the object param=”abc” created for the first time is passed as a parameter to the constructor of String. Equivalent to String str = new String(param).

—————The detailed analysis is as follows————

Analysis 1: We can divide the above line of code into the following four parts:

String str
new String()

String str just defines a variable name of type [String named str, so it does not create an object;]

= initializes the variable str, assigns a reference (or handle) of an object to it, and obviously does not create an object;

Now only new String(“abc”) is left.

Analysis 2: Then, why can new String(“abc”) be regarded as “abc” and new String()?

Let’s take a look at the String constructor we called:

public String(String original) { //other code … }

As we all know, we commonly use the following two methods to create an instance (object) of a class:

First, use new to create an object.

Second, call the newInstance method of the Class class , and use the reflection mechanism to create an object.

We created an object by calling the above constructor method of the String class using new and assigning its reference to the str variable. At the same time, we noticed that the parameter accepted by the called constructor method is also a String object , which is “abc”.

So we’re going to introduce another way to create a String object – the text enclosed in quotation marks.

This method is specific to String, and it is very different from the new method.

String str=”abc”;

Undoubtedly, this line of code creates a String object.

String a=”abc”; String b=”abc”; What about here?

The answer is still one.

String a=”ab”+”cd”; look here again?

The answer is three.

Having said that, we need to introduce a review of the knowledge related to string pools .

There is a string pool in the JAVA virtual machine (JVM), which saves many String objects and can be shared, so it improves the efficiency. Since the String class is final , its value cannot be changed once it is created, so we don’t have to worry about program confusion caused by String object sharing. The string pool is maintained by the String class, and we can call the intern() method to access the string pool.

Let’s look back at String a=”abc”;, when this line of code is executed, the JAVA virtual machine first looks in the string pool to see if there is already such an object with the value”abc”. Its judgment is based on The return value of the equals(Object obj) method of the String class. If there is, no new object will be created, and the reference of the existing object will be returned directly; if not, the object will be created first, then added to the string pool, and then its reference will be returned. Therefore, it is not difficult to understand why the first two of the previous three examples are the answer.

Only new objects created by using “+” concatenation between String objects created by using quotation marks to contain text will be added to the string pool. For all “+” connection expressions that contain new objects (including null) in the new method, the new objects generated by them will not be added to the string pool , and we will not repeat them. Therefore, we advocate the use of quotation marks to include text to create String objects to improve efficiency , which is actually what we often use in programming.

Stack: It mainly saves basic types (or built-in types) (char, byte, short, int, long, float, double, boolean) and references to objects. Data can be shared, and the speed is second only to registers. faster than heap.

Heap: used to store objects

Leave a Comment

Your email address will not be published. Required fields are marked *