Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of String.intern () in java

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you the example analysis of String.intern () in java, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Let's start with a few questions:

Q: the output of the following programs:

String S1 = "abc"

String S2 = "abc"

System.out.println (S1 = = S2)

A:true, both pointing to objects in the constant pool.

Q: the output of the following programs:

String S1 = new String ("abc")

String S2 = new String ("abc")

System.out.println (S1 = = S2)

A:false, two references point to different objects in the heap.

Q: the output of the following programs:

String S1 = "abc"

String S2 = "a"

String S3 = "bc"

String S4 = S2 + S3

System.out.println (S1 = = S4)

A:false, because s2+s3 is actually done using StringBuilder.append, generates different objects.

Q: the output of the following programs:

String S1 = "abc"

Final String S2 = "a"

Final String S3 = "bc"

String S4 = S2 + S3

System.out.println (S1 = = S4)

A:true, because the final variable is directly replaced with the corresponding value after compilation, so it is actually equal to S4 = "a" + "bc", in which case the compiler merges directly to S4 = "abc", so the final s1==s4.

Q: the output of the following programs:

String s = new String ("abc")

String S1 = "abc"

String S2 = new String ("abc")

System.out.println (s = = s1.intern ())

System.out.println (s = = s2.intern ())

System.out.println (S1 = = s2.intern ())

A:false,false,true .

How many questions did you get right?

To understand String.intern (), there are a few rules:

First, new String creates string objects on the heap. When the intern () method is called, the compiler adds a string to the constant pool (stringTable maintenance) and returns a reference to the constant.

Second, when creating a string (such as String str= "twm") through literal assignment, it will first find out whether the same string exists in the constant pool, and if so, point the reference in the stack directly to the string; if not, generate a string in the constant pool, and then point the reference in the stack to the string.

Third, the "+" operation of a constant string, the compilation phase is directly merged into a string. For example, string str= "JA" + "VA" will merge the idiom String str= "JAVA" directly during the compilation phase, so it will look for the existence of "JAVA" in the constant pool to create or reference it.

4. For final fields, constant substitution is performed directly at compile time (while non-final fields are assigned at run time).

Final String str1= "ja"

Final String str2= "va"

String str3=str1+str2

At compile time, it is directly replaced by String str3= "ja" + "va", and again by String str3= "JAVA" according to the third rule.

Fifth, when constant strings and variables are concatenated (such as: String str3=baseStr + "01";), stringBuilder.append () is called to create a new object on the heap.

6. After JDK 1.7, the intern method will still query whether it already exists in the constant pool, and if so, return the reference in the constant pool, which is no different from before. The difference is that if the corresponding string cannot be found in the constant pool, the string will not be copied to the constant pool, but will only generate a reference to the original string in the constant pool. To put it simply, what you put into the constant pool has changed: when you can't find it in the constant pool, copy a copy to the constant pool, and then copy the address reference on the heap to the constant pool after 1.7.

Examples are as follows:

String str2 = new String ("str") + new String ("01")

Str2.intern ()

String str1 = "str01"

System.out.println (str2==str1)

Under JDK 1.7, when str2.intern (); is executed, because there is no string "str01" in the constant pool, a reference to the "str01" in the heap is generated in the constant pool (note that this is the reference, which is the difference from JDK 1.6). Under JDK1.6, a copy of the original string is generated, but when String str1 = "str01" is assigned, there is already a reference in the constant pool, so the reference is returned directly, so both str1 and str2 point to the same string in the heap and return true.

String str2 = new String ("str") + new String ("01")

String str1 = "str01"

Str2.intern ()

System.out.println (str2==str1)

After swapping the middle two lines, because the constant pool does not exist in the literal quantity assignment (String str1 = "str01"), str1 points to the location in the constant pool, while str2 points to the object in the heap. When you do the intern method, it has no effect on str1 and str2, so return false.

The above is all the content of the article "sample Analysis of String.intern () in java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report