String data type (EXT) in Java __java

Source: Internet
Author: User
Tags stringbuffer

1. First, string does not belong to 8 basic data types, and string is an object.

Because the default value of the object is NULL, the default value of string is also null, but it is a special object with some attributes that other objects do not have.

2. The new string () and the new string ("") are all declarations of a newly empty string, which is not null;

3. String str= "Kvill";
String Str=new string ("Kvill"), the difference:

Here, we don't talk about heaps, we don't talk about stacks, we simply introduce the simple concept of a constant pool.

Chang (constant pool) refers to some data that is determined at compile time and is saved in the compiled. class file. It includes constants in classes, methods, interfaces, and so on, as well as string constants.

See Example 1:

String s0= "Kvill";
String s1= "Kvill";
String s2= "kv" + "ill";
System.out.println (S0==S1);
System.out.println (S0==S2);


The results are:

True
True


First, we need to know that Java ensures that there is only one copy of a string constant.

Because the "Kvill" in the S0 and S1 in the example are string constants, they are determined at compile time, so s0==s1 is true, and "kv" and "ill" are string constants, and when a string is concatenated by multiple string constants, it is definitely a string constant, So S2 is also parsed as a string constant at compile time, so S2 is also a reference to "Kvill" in a constant pool.

So we come to s0==s1==s2;

A string created with new string () is not a constant and cannot be determined at compile time, so the string created by new string () is not placed in a constant pool and has its own address space.

See Example 2:

String s0= "Kvill";
String S1=new string ("Kvill");
String s2= "kv" + New String ("ill");
System.out.println (S0==S1);
System.out.println (S0==S2);
System.out.println (S1==S2);


The results are:

False
False
False


Example 2, S0 or the application of "Kvill" in a constant pool, S1 is a reference to the new object "Kvill" that was created at run time because it cannot be determined at compile time, S2 is also a newly created object because it has the latter half of new String ("ill") and cannot be determined at compile time. Kvill "The application of the", understand these also know why to come to this result.

4. String.intern ():

One more thing: the constant pool that exists in the. class file is loaded by the JVM at runtime and can be expanded. The Intern () method of string is a method of extending a constant pool, and if a string instance str calls the Intern () method, does the Java lookup constant pool have the same Unicode string constant, and if so, returns its reference, if not, Adds a string of Unicode equals str to the constant pool and returns its reference; see example 3 is clear.

Example 3:

String s0= "Kvill";
String S1=new string ("Kvill");
String S2=new string ("Kvill");
System.out.println (S0==S1);
System.out.println ("**********");
S1.intern ();
S2=s2.intern (); Assigns a reference to "Kvill" in a constant pool to S2
System.out.println (S0==S1);
System.out.println (S0==s1.intern ());
System.out.println (S0==S2);


The results are:

False
**********
False//Although S1.intern () was executed, its return value was not assigned to S1
True//Description S1.intern () returns a reference to "Kvill" in a constant pool
True


Finally, I will break a wrong understanding:

Some say, "Using the String.intern () method, you can save a string class to a global string table, and if the Unicode string with the same value is already in this table, the method returns the address of the string already in the table. If a string with the same value is not in the table, register your address in the table "If I had understood this global string table as Chang, his last remark," If you don't have a string with the same value in the table, registering your address in the table is wrong:

See Example 4:

String S1=new string ("Kvill");
String S2=s1.intern ();
System.out.println (S1==s1.intern ());
System.out.println (s1+ "" +s2);
System.out.println (S2==s1.intern ());


Results:

False
Kvill Kvill
True


In this class we have no reputation for a "kvill" constant, so there's no "Kvill" at first in the constant pool, and when we call S1.intern () we add a new "Kvill" constant in the constant pool, and the original "Kvill" in the constant pool still exists, and it's not " Register your address in a constant pool ".

S1==s1.intern () to False indicates that the original "Kvill" still exists;

S2 is now the address of "Kvill" in a constant pool, so S2==s1.intern () is true.

5. About Equals () and = =:

This is simple for string to compare the Unicode sequences of two strings to be equivalent, if equality returns true, and = = is to compare the addresses of two strings to the same, that is, whether they are references to the same string.

6. About string is immutable

This is a lot to say, you just know that a string instance once generated will not change again, for example: string str= "kv" + "ill" + "" + "ans";
Is that there are 4 string constants, first "KV" and "ill" generate "Kvill" in memory, and then "Kvill" and "" Generate "Kvill" exist in memory, and then "Kvill ans" is generated, and the address of this string is assigned to STR, Because string's "immutable" produces a lot of temporary variables, which is why it is recommended to use StringBuffer because StringBuffer is changeable.

Turn from: http://dev.yesky.com/91/2309091.shtml

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.