Java String class detailed __string class

Source: Internet
Author: User
Tags garbage collection
Java String Class (Java.lang.String) is the most used class in Java, and is also the most special class, and many times we are both familiar and unfamiliar with it.

A fundamental understanding of the Java.lang.String class and string pool

First, I recommend that you look at the string class's source code implementation, which is the fundamental starting point of knowing the string class in essence. From here you can see:
1. The string class is final and cannot be inherited. Public final class String.
2, the String class is the essence of the character array char[], and its value can not be changed. PRivate final char value[];
You then open the API documentation for the string class to discover:
3. A String class object has a special way of creating, that is, directly specifying such as String x = "abc", and "ABC" representing a String object. And X is the address of the "ABC" object, also called

Make a reference to the "ABC" object.
4. String objects can be concatenated by "+". A new string is generated when the concatenation occurs. It can also be concatenated by concat (), which is described later.
6. The Java runtime maintains a string pool (string pooling), and Javadoc translation is very vague "string buffers." String pools are used to store the various strings generated during the runtime,

and the contents of the string in the pool are not duplicated. The general object does not exist this buffer pool, and the object created exists only in the stack area of the method.


5, create a string of many ways, summed up there are three categories:
First, use the New keyword to create a string, such as String S1 = new String ("abc");
Second, direct designation. such as String s2 = "abc";
Third, a new string is generated using concatenation. For example, string s3 = "AB" + "C";

Second, the creation of a string object

String objects are also very important to create, and the key is to understand their rationale.
Principle 1: When you use any way to create a string object s, the Java runtime (the running JVM) takes this x in the string pool to find out if there is a string object with the same content.

If it does not exist, a string s is created in the pool, otherwise it is not added in the pool.

In principle 2:java, whenever you create an object using the New keyword, you must create a new object (in the heap area or stack area).

Principle 3: To create a string object by using either direct designation or concatenation with a pure string, only the string that is maintained in the string pool is checked, and no one is created in the pool in the pool.

Out. But you never create the string object in the stack area again.

Principle 4: Creating a String object with an expression that contains a variable not only checks the maintenance string pool, but also creates a string object in the stack area.

In addition, the Intern () method of String is a local method defined as public native String intern (); The value of the intern () approach is to allow developers to focus on

On a string pool. When the Intern method is invoked, if the pool already contains a string equal to this string object determined by the Equals (object) method, the pool is returned

The string in. Otherwise, this string object is added to the pool and a reference to this string object is returned.

Three, immutable class
An immutable string has a great advantage: The compiler can set the string to be shared.
Immutable class strings have an important advantage-they are not shared references.

So, Java for efficiency, so special handling of string types--provides a string pool for string types
There are two ways to define a variable of type string:
String name= "Tom";
String name =new string ("Tom")
When you use the first method, you use a string pool,
When using the second way, it's a common way of declaring objects
If you use the first method, then when you declare a string that is also "Tom," it will use the original memory in the string pool without reallocating the memory, which means that string saname= "Tom" will point to the same memory

Another question about the string type is immutable:
The string type is immutable, that is, when you want to change a string object, such as Name= "Madding"
Instead of changing the original object, the virtual machine generates a new string object and then lets name point to it, and if the original "Tom" has no object to refer to it, the garbage collection mechanism of the virtual machine will receive it.
It's said to improve efficiency ...

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.