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

How to use the String.intern method

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article is about how to use the String.intern method. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Record the two ways to create a String, the difference between "" and new String (), the use of the String intern method, and the constant pool.

Use of String (Jdk1.8) code package com.com.string;/** * @ Auther: lantao * @ Date: 2019-04-15 13:53 * @ maill: lan_tao@suixingpay.com * @ Description: TODO * / public class StringTest {public static void main (String [] args) {/ / using "" to create String a = "lantao" directly in the constant pool / / create using new String, store zahngsan in constant pool, then create an object in Heap pointing to b String b = new String ("zhangsan"); / / use string concatenation to store 'wangwuzhaoliu' string directly in constant pool String c = "wangwu" + "zhaoliu" / / use string "reference" concatenation, do not execute the intern method, and will not store it in the constant pool, but String d = a + "- -" in the constant pool. / / use new String stitching, do not execute the intern method, and will not store the wang and jiu strings in the constant pool, but will store the wang and jiu strings in the constant pool String f = new String ("wang") + "jiu" / / use new String concatenation, do not execute the intern method, and will not store the zhao and ba strings in the constant pool, but will store the zhao and ba strings in the constant pool String g = new String ("zhao") + new String ("ba");}}

Explain String splicing

String concatenation

Public class StringTest {public static void main (String [] args) {String a = "lan" + "tao";}}

Both lan and tao are strings that are known to the compiler. The compiler will optimize this line of code. When a string is made up of multiple known strings (non-reference strings), it will be optimized as follows.

Public class StringTest {public static void main (String [] args) {String a = "lantao";}}

JVM puts the string "lantao" into the String constant pool.

Reference stitching:

Public class StringTest {public static void main (String [] args) {String a = "lan"; String b = a + "tao"; / / same meaning String c = "zhang"; String d = "san"; String f = c + d;}}

When the Java compiler encounters a string reference or string reference and knowable string concatenation, it creates a StringBuilder object followed by append ().

Because there is a string reference, the value of the reference cannot be determined at program compilation time. In addition, "lan" and "tao" are added by the compiler to the string constant pool (if not) because they are string constants determined at compile time, but the final "lantao" is not added to the string constant pool unless the b.intern () method is executed

Final splicing

Public class StringTest {public static void main (String [] args) {final String a = "lan"; final String b = "tao"; String c = a + b + "2019";}}

The difference between final splicing and the above two is that the final modification is added in front. The string modified with final is known at the compilation time, and the above code will be optimized to

Public class StringTest {public static void main (String [] args) {String str = "lantao2019";}}

Here the effect of final stitching is the same as string concatenation.

The variable a: "lantao" is a string constant, which is determined at compile time. First check whether there is a "lantao" string in the string constant pool. If not, add "lantao" to the string constant pool and point directly to it. So a points directly to the "lantao" of the string constant pool, that is, the address the variable a points to is the lantao in the constant pool.

Variable b: strings created with new String () are not constant and cannot be determined at compile time, so strings created by new String () are not put into the constant pool, they have their own address space (in Java Heap), and the referenced address of variable b is in Java Heap. But "zhangsan" string constants are also added to the string constant pool at compile time (if the constant pool does not exist).

Variables c: "wangwu" and "zhaoliu" are also string constants, when a string is concatenated by multiple string constants, it must also be a string constant, in the compiler will be optimized by the compiler "wangwuzhaoliu", so c is also parsed as a string constant at compile time, and c is a reference to "wangwuzhaoliu" in the constant pool, so the reference address of variable c is in the constant pool.

Variable d: JVM for string references, because there is a string reference in the "+" connection of the string, and the value of the reference cannot be determined at program compilation time, that is, `(a + "- -")

Variable f: variable f cannot be determined at compile time either, but the string constants "wang" and "jiu" are added to the string constant pool and the String object is created in the heap. (the string constant pool does not hold the string "wangjiu" unless the f.intern () method is executed.)

Variable g: the same variable f.

Detailed explanation of String#intern method intern method

String.intern () is a Native (local) method that returns a reference to this string in the string constant pool if the string constant pool already contains a string equal to this String object, otherwise the reference address (in the heap) of the current String object is added to the string constant pool and returned.

Use

Note: the = = between basic data types is the comparison value, and the reference data type = = is the address value.

The string public class StringTest {public static void main (String [] args) {/ / between basic data types is a comparison value, and the reference data type = = compares the address value / / 1: create an object in Java Heap 2: add zhangsan String a = new String ("zhangsan") to the string constant pool / / call the intern method. Since zhangsan has been stored in the constant pool in the previous step, the reference address String b = a.intern () of the constant pool zhangsan is returned directly; / / the address of an is in the Java Heap, and the address of b is in the constant pool, so the result is flase System.out.println (a = = b) / / because the constant pool already contains zhangsan, it directly returns String c = "zhangsan"; / / b c has the same address, so it is true System.out.println (b = = c);}} / / the result falsetrue

Explanation:

1: create an object in Java Heap and then add zhangsan to the string constant pool.

2: call the intern method. Since zhangsan has been stored in the constant pool in the previous step, the reference address of the constant pool zhangsan is returned directly.

3: because the address of an is in Heap and the address of b is in the string constant pool.

4: because the constant pool already contains zhangsan, it returns directly

5: B / c has the same address, so it is true

The address can be obtained using the System.identityHashCode (a) method

The string public class StringTest {public static void main (String [] args) {/ / 1: first creates an object in Heap, then puts zhagnsan and wangwu in the constant pool, but does not put zhagnsanwangwu String a = new String ("zhangsan") + "wangwu" in the constant pool / / 2: intern is called because there is no concatenated string such as "zhangsanwangwu" in the string constant pool, so the reference address of the String object in the heap is added to the string constant pool. After jdk1.7, the constant pool is introduced into Heap, so you can directly store the reference String b = a.intern (); / / 3: because the address of an is the same as that of b, the lock is true System.out.println (true System.out.println); / / 4: because zhangsanwangwu already exists in the constant pool, the direct return reference is a type of an a==b==c String b lock c = "zhangsanwangwu" System.out.println (a = = c); / / true System.out.println (b = = c); / / true / / 5: objects are first created in Heap, and then zhang and san String d = new String ("zhang") + "san" are stored in the constant pool / / 6: the address in the constant pool is returned, because zhangsan has been put into the constant pool in the a variable String f = d.inter (); System.out.println (d = f); / / false}}

Explanation:

1: object an is first created in Heap, and then zhagnsan and wangwu** are placed in the constant pool, but not zhagnsanwangwu.

2: call intern, because there is no concatenated string such as "zhangsanwangwu" in the string constant pool, so add the reference address of the String object in the heap to the string constant pool. Constant pools are introduced into Heap after jdk1.7, so references can be stored directly.

3: because the address of an is the same as that of b, it is true.

4: since zhangsanwangwu already exists in the constant pool, the direct return reference is of type a, so it is a==b==c.

5: object d is first created in Heap, and then zhang and san are stored in the constant pool.

6: since "zhangsan" has been put into the constant pool when object an is created, the zhangsan address in the constant pool is returned. The address of the object d is in the Heap, and the address of f is in the constant pool, not one, so false

Thank you for reading! This is the end of this article on "how to use the String.intern method". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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