Java string string in-depth explanation

Source: Internet
Author: User

There are two forms of string object creation in Java, one for literal form, such as String str = "Hello", and the other is to use the new standard method of constructing objects, such as String str = new string ("Hello");

For this kind of common sense, no longer repeat.

First, the string class is the final class, why is it defined in final form?

To put it simply, for data types that are used at such high frequencies, designers think that they have been designed to be good enough to be inherited, or that random inheritance overrides can degrade the performance of the program.

As the title describes, as we go deeper, we dig down the string at the JVM level.

Explain the form of literal creation first:

When a string object is created in the literal form of the code, the JVM first checks the literal, and if a reference to a string object of the same content exists in the string constant pool, the reference is returned, otherwise the new string object is created, then the reference is placed into the string constant pool, and the reference is returned.

As shown below:

String str1 = "Hello";

When we first created this, we thought there was no content for the Hello object. The JVM finds a string object with a string constant pool that does not exist, then creates the string object and then puts a reference to the object you just created into the string constant pool and returns the reference to the variable str1

If there's a piece of code that follows

String str2 = "Hello";

The JVM also detects this literal, and the JVM returns the reference to the variable str2 by finding the string constant pool and discovering that the content is present as a "hello" string object. Note that new string objects are not recreated here .

To verify that str1 and str2 point to the same object, we can pass this code

System.out.println (str1 = = str2);

The result is true.

The second is created using new:

New String ("Hello");

When we use new to construct a string object, a new string object is created, regardless of the reference to the object with the same content in the string constant pool. So let's test it with the code below,

New String ("Hello"= = STR3);

The result is false. Describes the two references pointing to different objects.

Intern

For the string object created above using new, you can use the Intern method if you want to add a reference to this object to the string constant pool.

After calling intern, first check to see if there is a reference to the object in the string constant pool, and if so, return the reference to the variable, otherwise the reference is added and returned to the variable.

String STR4 == = = str1);

The result is true.

Difficult issues

Prerequisites?

The precondition of a string constant pool implementation is that the string object in Java is immutable, which ensures that multiple variables share the same object safely. If a string object in Java is mutable, and a reference operation alters the value of the object, then the other variables are affected, which is obviously unreasonable.

Referencing or objects

The problem is most common in strings that are stored in a constant pool as references or objects. A string constant pool holds an object reference, not an object. In Java, objects are created in heap memory. A string constant pool exists in the heap memory for a permanent generation

Advantages and Disadvantages

The advantage of a string constant pool is to reduce the creation of the same content string, saving memory space.

If hard to say the disadvantage, it is to sacrifice the CPU computing time to change space. CPU compute time is primarily used to look up a reference in a string constant pool for the content of the same object. However, its internal implementation is Hashtable, so the computational cost is lower.

GC Recycling?

Because a string constant pool holds a reference to a shared string object, does that mean that these objects cannot be recycled?

The objects that are shared in the first question are generally small. As far as I am aware, this problem does exist in earlier versions, but with the introduction of weak references, the problem should not be present.

Intern use?

The premise of using intern is that you know you really need to use it. For example, we have a record of millions of records, one of which has been recorded several times in the state of California, and we do not want to create a string object like millions, and we can use intern to keep only one copy in memory.

There's always an exception?

Do you know the following code that creates several string objects and saves a few references in a string constant pool?

String test = "a" + "B" + "C";

The answer is that only one object is created and only one reference is saved in the constant pool. We can see it by using JAVAP anti-compilation.

In fact, during compilation, these three literals have been synthesized into one. This is actually an optimization, avoiding the creation of redundant string objects, and the problem of string stitching does not occur.

Java string string in-depth explanation

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.