String S = new string ("ABC"). How many objects are created?

Source: Internet
Author: User

Reprinted from: http://www.cnblogs.com/ydpvictor/archive/2012/09/09/2677260.html

String STR = new string ("ABC ");CodeThis problem is often followed by how many string objects have been created in this line of code?

I believe everyone is familiar with this question, and the answer is also well known.

Next, let's start with this question and review some java knowledge related to creating string objects.

We can divide the above line of code into four parts: String STR, =, "ABC", and new string. String STR only defines a variable of the string type named STR, so it does not create an object; = is to initialize the variable STR, reference an object (or handle) assign a value to it, and apparently no object is created; now only new string ("ABC") is left. So 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...} we all know that there are two common methods to create an instance (object) of a class:

1. Use new to create an object.

2. Call the newinstance method of the class and use the reflection mechanism to create an object.

We use new to call the constructor method of the string class to create an object and assign 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 exactly "ABC ". Therefore, we need to introduce another way to create a String object-enclose text in quotation marks.

 

This method is unique to a string and 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"; here again?

There are three answers.

Here, we need to review the knowledge about the string pool.

A string pool exists in the Java Virtual Machine (JVM), which stores many string objects and can be shared. Therefore, it improves the efficiency. Because the string class is final, its value cannot be changed once it is created, so we don't have to worry about the sharing of string objects.ProgramChaos. The string pool is maintained by the string class. 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 searches the string pool to see if such an object with a value of "ABC" already exists, it is determined based on the return value of the string equals (Object OBJ) method. If yes, no new object is created and a reference to an existing object is directly returned. If no, this object is created first and then added to the string pool, then return its reference. Therefore, it is difficult to understand why the first two examples in the preceding three examples are the answer.

 

Only new objects created using the "+" connection between string objects created using quotation marks containing text are added to the string pool. For all "+" join expressions that contain new-type new objects (including null), the new objects generated by the new expressions are not added to the string pool. We will not repeat this. Therefore, we advocate that you use quotation marks to contain text to create string objects to improve efficiency. In fact, this is often used in programming.

 

STACK: stores basic types (or built-in types) (char, byte, short, Int, long, float, double, Boolean) and object references, data can be shared, which is faster than the register.

Heap: used to store objects.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.