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

What are the characteristics of the string type String of Java

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the relevant knowledge of "what are the characteristics of Java string type String". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "what are the characteristics of Java string type String" can help you solve the problem.

I. concept

String stands for a string, and all double-quoted strings in the java language are String objects, regardless of whether they are new objects or not.

2. Characteristics

The 1.String class cannot be inherited because it is modified by final. Note that at first I thought that the string could not be changed because it was modified by final. In fact, this is not the case. The bottom layer of String is saved with an char array. It is modified by final but cannot change the address direction, but its content can be changed. So the string cannot be changed regardless of whether it is modified by final or not.

two。 Strings can be shared because they cannot be changed.

3. The underlying string is stored using the char [] array.

Third, there are three ways of construction:

1.public String (): create an empty string object

2.public String (char [] array): creates a string using the char array

3.public String (byte [] array): use the byte array to create a string, which converts the corresponding numbers into characters according to the ASCII code, such as 97 for the character a

A direct creation: String str = "abc"

String constant pool

Before jdk1.6, the string constant pool was in the method area, and the jdk1.7 and later string constant pools were separated from the method area and stored in the heap.

What jdk1.6 and jdk1.7 have in common:

1. Creating strings with double quotes creates string objects directly in the string constant pool at JVM runtime, but does not create objects in the heap

2.new a string object (String s = new String ("abc")), which involves the creation of two objects, one is the heap object, and the other is the object in the string constant pool ("abc" is in double quotes). If the string constant pool has the same object as the string (in this case, the same content, not the address), you only need to create a heap object, if the string constant pool does not have the same object. Then you need to create objects in both the string constant pool and the heap. Note that there is no reference between the two objects, that is, the value of one object is the address of the other object.

String test1 = "test"; String test3 = "test"; String test2 = new String ("test"); System.out.println (test1 = = test2); / / false System.out.println (test1 = = test3); / / true

The first output is false because test1 is an object in the string constant pool and test2 is an object in the heap, so their addresses are not equal, so the result is false The second output is true because when you create a string with double quotes, you will first go to the string constant pool to find out whether there is an object with the same value, return the reference address directly if it exists, and create the object if it does not exist, so test1 and test3 point to the same address.

The differences between jdk1.6 and jdk1.7:

The character creation constant pool storage of jdk1.6 is an object, and the jdk1.7 character constant pool can store both objects and references to objects.

One more thing needs to be clear:

New objects generated by using "+" connections between String objects created with quotation marks containing text will be added to the string pool. for all "+" connection expressions that contain new objects created by new (including null), the new objects it produces will not be added to the string pool.

String S6 = new String ("go") + new String ("od"); String S7 = s6.intern (); String S8 = "good"; System.out.println (S6 = = S7); / / true System.out.println (S7 = = S8); / / true System.out.println (S6 = = S8); / / true

The above introduction on the right shows that S6 points to the address of the string good object in the heap, and this object is not created in the string constant pool, when executed to String S7 = s6.intern () Because the character creation constant pool does not have this object, and the object exists in the heap, create a reference in the string constant pool that points to the object in the heap, so S6 and S7 point to the same object. If it is jdk1.6, they will directly create an object in the string constant pool and return a reference to this object. In this case, S6 and S7 point to different objects.

String S2 = new String ("lo") + new String ("ng"); String S3 = s2.intern (); System.out.println (S2 = = S3); / / false

True should be returned here according to the above analysis, but false should be returned here.

This is the end of the content about "what are the characteristics of the string type String of Java". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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

Development

Wechat

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

12
Report