Java String and constant pool, JavaString constant pool

Source: Internet
Author: User

Java String and constant pool, JavaString constant pool

String is a string in java. The String class is unchangeable. Any change to the String class will return a new String class object.

1. First, String does not belong to eight basic data types. String is an object.

Because the default value of an object is null, the default value of String is also null, but it is a special object and has some features that other objects do not have.

2. Both new String () and new String ("") declare a new null String, which is an empty String or not null;

3. String str = "kvill ";String str = new String ("kvill"); difference:

Here, we will not talk about heap or stack, but simply introduce the simple concept of constant pool.

The constant pool refers to the data that is identified during the compilation period and saved in the compiled. class file. It includes constants in classes, methods, interfaces, and other fields, as well as string constants.

String s0=”kvill”;  String s1=”kvill”;  String s2=”kv” + “ill”;  System.out.println( s0==s1 );  System.out.println( s0==s2 );

Result:

true   true

First, we need to know that Java will ensure that a String constant has only one copy.

Because "kvill" in s0 and s1 in the example are string constants, they are determined during the compilation period, so s0 = s1 is true; "kv" and "ill" are both string constants. When a string is connected by multiple string constants, it must be a String constant, so s2 is also parsed as a String constant during compilation, so s2 is also a reference of "kvill" in the constant pool.

So we get s0 = s1 = s2;

The String created with new String () is not a constant and cannot be determined during the compilation period. Therefore, the strings created with new String () are not placed in the constant pool and they have their own address space.

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 );

Result:

false  false  false

S0 is still a "kvill" application in the constant pool. s1 is a reference of the new object "kvill" created at runtime because it cannot be determined during the compilation period, s2 is also an application that creates the "kvill" object because it has a new String ("ill") in the second half and cannot be determined during the compilation period; if you understand this, you will know why this result is obtained.

4. String. intern ():

Another note: the constant pool exists in the. class file and is loaded by JVM at runtime and can be expanded. The intern () method of String is a method to expand the constant pool. When a String instance str calls the intern () method, Java checks whether the constant pool has the same Unicode String constant, if yes, its reference is returned. If no, a string with Unicode equal to str is added to the constant pool and Its Reference is returned;

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 (); // assign the reference of "kvill" in the constant pool to s2 System. out. println (s0 = s1); System. out. println (s0 = s1.intern (); System. out. println (s0 = s2 );

Result:

False ********** false // although s1.intern () is executed, its return value is not assigned to s1 true // meaning s1.intern () the return value is the reference of "kvill" in the constant pool.

Finally, let me get rid of another misunderstanding:

Someone said, "Use String. the intern () method can save a String class to a global String table. If a Unicode String with the same value is already in this table, this method returns the address of an existing String in the table, if there is no String with the same value in the table, register the address in the table. "If I understand the global String table he said as a constant pool, his last sentence is, "If there is no string with the same value in the table, register your address in the table" is incorrect:

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() );

Result:

false kvill kvill  true

In this class, we do not have a "kvill" constant, so there is no "kvill" in the constant pool at the beginning. When we call s1.intern () then, a new "kvill" constant is added to the constant pool. The original "kvill" that is not in the constant Pool still exists, so it is not "registering your own address to the constant pool.

If s1 = s1.intern () is false, the original "kvill" still exists;

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

5. About equals () and =:

In simple terms, this is to compare whether the Unicode sequence of the two strings is equivalent. If the two strings are equal, true is returned, and = is to compare whether the addresses of the two strings are the same, that is, whether it is a reference to the same string.

6. the String is immutable.

This is a lot more important, as long as you know that the String instance will not change once it is generated, for example: String str = "kv" + "ill" + "" + "ans ";
There are four string constants. First, "kv" and "ill" generate "kvill" to exist in the memory, and then "kvill" and "generate" kvill "to exist in the memory, finally, "kvill ans" is generated, and the address of this String is assigned to str, because the String's "immutable" produces many temporary variables, this is why StringBuffer is recommended, because StringBuffer can be changed.

I am the dividing line of tiantiao

 

 

Reference: http://developer.51cto.com/art/201106/266454.htm


What is the java constant pool?

Int a = 1; 1 will be stored in the stack
String s1 = "thanks"; thanks will be stored in the data segment
String s2 = new String ("thanks"); thanks will be stored in the data segment
String a = "th"; String B = "anks"; String c = a + B; c will be stored in data segment
All strings are stored in data segment.
The constant pool is all in the stack.
If you are not new, for example, if you write String s = "thanks", s is allocated in the stack, and s points directly to the "thanks" in the character pool, string s; s = new String ("thanks"); then s points to a piece of memory in the heap, and the address in the memory points to the "thanks" in the character pool ";
What's the matter!

What are the eight basic types and strings stored in the java constant pool? Are there other types?

Baike.baidu.com/..cr_7i _

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.