Java thousand ask _06 data Structure (020) _string is what

Source: Internet
Author: User
Tags stringbuffer

Click to enter _ more _java thousand ask

1. What is a string

The Java.lang.String class represents a string that is usually bright, so-called string, which is a set of characters composed of character sets. It has the following characteristics:

String is an immutable object
Each time a string is changed, it is actually equivalent to generating a new string object, and then pointing the pointer to the new string object (if you do not use the new construct, you are actually looking from the string pool in the heap to see if the string has been saved, and if so, point directly to the If not, put the string into the string pool and point to it first.
Therefore, it is best not to use string to change the content of the strings, each generation of the object will have an impact on system performance (especially when there is no reference object in the heap, the JVM garbage collection GC will start to work, performance will be affected).

Learn about garbage collection here: [What is the Java garbage collection mechanism][2]
Learn how string is stored in memory see here: How string is stored in memory

Comparison between string and stringbuffer efficiency
In some special cases, the string object will not change at a slower rate than the StringBuffer object, especially in the following string object generation, where string efficiency is far faster than StringBuffer:

String"hello ""world"new"hello ""world" );

You'll be surprised to find that the speed at which the string object was generated was simply too fast, and at this time StringBuffer had no advantage at all.
In fact, this is a JVM trick, in the JVM's eyes, this

String"hello ""world" ;

is equivalent to:

String"hello world" ;

So it doesn't take much time. Note, however, that if your string is from another string object, the JVM will behave in a way that creates several objects, such as:

String"hello " ;String"world" ;String"hello world" ;

String also has two similar common classes, stringbuffer/stringbuilder, to understand their use and differences look here: Stringbuffer/stringbuilder What's the Difference

Java thousand ask _06 data Structure (020) _string is what

Related Article

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.