String feature parsing and String feature Parsing

Source: Internet
Author: User

String feature parsing and String feature Parsing
String

1. Immutable

String is the object of the encapsulated Char array.
Char [] a = {'h', 'E', 'l', 'l', 'O '};
String s = new String ();

String s = "hello ";

In memory, data stored in arrays is essentially data. The array length is immutable, and the created String object is immutable, but the variable is variable.
The value of s variable can be changed, and the string "hello" is immutable.

2. Comparison
String is a reference type. Use = to determine whether it is the same object.
All operations performed by using symbols are completed in the stack memory, such as "=" and "= ".
Therefore, compare whether the referenced address or memory address is equal

Because the strings are non-mutable, It is very memory-consuming to create strings repeatedly. Therefore, in the memory, a specific area is specified to store various constants, in the constant area, there is a special area to store string constants, called the String constant pool. It has a feature that strings are unique.

For example,
String s1 = "hello ";
String s2 = "hello ";
S1 = s2 true

When s1 is created, the string constant pool has one "hello". When s2 is created, the constant pool is no longer created because it is also "hello, s2 points directly to the created constant

So s1 = s2 true

String s3 = new String ("hello ");
String s4 = new String ("hello ");

As long as you see the object created by new, it must be in the heap memory. new indicates applying for a new space for storage.

S1 = s3 false
S3 = s4 false

S5 = "he" + "llo ";
S6 = "he ";
S6 + = "llo ";
S7 = s1;

S5 is obtained by adding two constants, constant + constant = constant. This process is completed directly during the compilation process and does not need to be executed. Therefore, when the program is running, s5 is equal to "hello"

So s1 = s5 true

The s6 value is a variable obtained from a variable + constant. For a program, the variable is known only when the program is running, and the value obtained at this time is definitely not in the constant pool. Therefore, the value produced by s6 is in the heap memory, while s1 is in the constant pool.

Therefore, s1 = s6 false

S7: Yes, the assignment directly assigns values to the reference address of s1.

So s1 = s7 true

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.