Creation and maintenance of string strings

Source: Internet
Author: User

Strings String2.1 String class

A string is an immutable class that, once a string object is created, the sequence of characters contained in the object is immutable until the object is destroyed.

The string class is the final class and cannot have subclasses.

2.1.1 Creating a String Object

1. Use the New keyword

string S1 = new String ("AB"); //

2. Use string constants to assign values directly

String s2 = "abc";

3. Use the "+" operator for string connections

String s3 = "abc" + "D";

String S4 = s3 + 5; Abcd5

Constant Pool Concepts:

The Java runtime maintains a string pool, also known as a string buffer. String The pool is used to store the various strings produced in the runtime, and the contents of the strings in the pool are not duplicated . The normal object does not exist in this buffer pool, and the object created only exists in the stack area of the method.

The creation of a string object is very elaborate, the key is to understand its principle.

Principle 1 : When using any way to create a string object s, the Java runtime (the running JVM) takes this s in the string pool to find out if there is a string object with the same content, and if not, creates a string s in the pool, otherwise it is not added in the pool.

Principle 2 : In Java, whenever you use the New keyword to create an object, a new object will be created (in the heap area).

Principle 3 : Creating a String object using either direct or string concatenation, only checks the string in the maintenance string pool and does not create one in the pool. However, the string object will never be created in the stack area again.

principle 4 : Creating a String object with an expression that contains a variable will not only examine the maintenance of the string pool, but also create a string object in the stack area. Finally point to the object in the heap memory

For example:

Public class stringtest {

Public Static void Main (String args[]) {

Create a String Object "ABC" in the pool and in the heap, S1 point to the object in the heap

string S1 = new string ("abc");

S2 direct point to the Pool object "ABC"

String s2 = "abc";

Creates a new "ABC" object in the heap, S3 points to the object

String s3 = New string ("abc");

Create the object "ab" and "C" in the pool, and S4 point to the object "ABC" in the pool

String S4 = "AB" + "C";

C points to the object "C" in the pool

String c = "C";

Creates a new object "ABC" In the heap, and S5 points to the object

String S5 = "AB" + C;

System. out. println ("------------Real string-----------");

System. out. println (S1 = = s2); False

System. out. println (S1 = = S3); False

System. out. println (s2 = = s3); False

System. out. println (s2 = = S4); True

System. out. println (S2 = = S5); False

}

}

Creation and maintenance of string strings

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.