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 create the String class of Java

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to create the String class of Java". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to create the String class of Java".

I. Preface

A string is a sequence of characters concatenated and combined. Strings are divided into mutable strings and immutable strings.

(1) immutable string: when the string object is created, the content of the object (the above character sequence) cannot be changed, and once the content changes, a new string object will be created. The objects of the String class in Java are immutable.

(2) variable string: the object of the StringBuilder class and the StringBuffer class is variable; when the object is created, the content of the object will not be created when the content of the object changes, that is to say, the content of the object can be changed. When the content of the object changes, the object remains the same, or the same.

String, StringBuffer, and StringBuilder all implement the CharSequence interface, and the string at the bottom is actually char []. Although they are all related to strings, their processing mechanisms are different.

II. String class (string constant)

The String class represents an immutable string, and after the current String class object is created, the content (character sequence) of the object is unchanged, because once the content changes, a new object is created.

The String class is a final class and cannot be inherited. The best way to reuse String types is to combine rather than inherit.

2.1 creation of String class instance

Method 1: create by literal quantity assignment. Note that there are double quotes: "", which is different from the single quotation marks of the character char type:''.

String S1 = "laofu"

Method 2: create through the constructor

String S2 = new String ("laofu")

The difference between the two ways:

Method 1: String S1 = "laofu"

It is possible to create only one String object, or not to create a String object; if "laofu" already exists in the constant pool, object S1 will reference it directly and will not create a new String object; otherwise, the memory space of the constant "laofu" will be created first in the constant pool and then referenced.

Method 2: String S2 = new String ("laofu")

A maximum of two String objects are created and at least one String object is created. Creating an object using the new keyword creates a memory region in the heap space, which is the first object; then the string literals in the object may create a second object, and the second object, as described in method 1, may not be created, so at least one String object is created.

The constant pool in the image above: the local memory area used to store constants, located in the method area. Constant pools are divided into compiled constant pools and running constant pools:

Compile constant pool: when bytecode is loaded into JVM, information about bytecode (such as line number, etc.) is stored.

Run the constant pool: the constant data in the code is stored.

Strings created by ① using string literals, that is, strings created with quotation marks alone, are direct quantities and are stored in the constant pool at compile time

Objects created by ② using new String ("") are stored in heap memory and created at run time

③ creates direct quantities using string concatenators that contain only direct quantities, such as "aa" + "bb". Such strings can be determined at compile time, so they are also stored in the constant pool.

Objects created by ④ using string expressions that contain String direct quantities, such as "aa" + S1, are created at run time and are stored in the heap because the underlying StringBuilder objects are innovated to implement stitching.

2.2 comparison of String objects

① uses the "=" sign to compare whether the memory address referenced by the object is the same.

② uses the equals method: it is the same as the "= =" sign in the Object class, but in the custom class, it is recommended to override the equals method to compare the details of its own content; because the String class overrides the equals method, it compares string content.

2.3 Null value of String object

The ① object reference is empty, so S1 is not initialized and no memory space is allocated in JVM.

String S1 = null

The content of the ② object is an empty string, for example, object S2 has been initialized, the value is "", and JVM has allocated memory space for it.

String S2 = ""; 2.4 string concatenation

Strings in Java can be concatenated by being "+", so how do strings in code be concatenated in JVM? Let's use an example to show how JVM handles string stitching by comparing the code before and after compilation of splicing string code.

JVM does some optimizations for string concatenation.

① if the concatenation between literals of a string (such as "aa" + "bb"), the direct quantity is also created, which can be determined at compile time, so it will also be stored in the constant pool.

If ② is stitching between objects, or between objects and literals, or the result of method execution participates in stitching, String will first use StringBuilder to obtain the value of the object, and then use the append method to perform stitching. In this case, the value of the variable and the return value of the method can only be determined at run time.

3. StringBuilder and StringBuffer (string variable)

Both StringBuffer and StringBuilder represent mutable strings, and both functions are the same. But the only difference:

(1) the methods in StringBuffer:StringBuffer all use the synchronized modifier to indicate the synchronous operation, which can ensure thread safety when multithreading is concurrent, but when ensuring thread safety, it will have a certain impact on its performance and will reduce its performance.

(2) none of the methods in StringBuilder:StringBuilder use the synchronized modifier, and the thread is not safe. Because of this, its performance is high.

In cases where there are no high requirements for concurrency security, StringBuilder is recommended because of its high performance.

4. String, StringBuilder and StringBuffer

(1) because the operation of the String class is to generate new String objects, and StringBuilder and StringBuffer are just the expansion of an array of characters, the operation of the String class is much slower than that of StringBuffer and StringBuilder.

In most cases: StringBuilder > StringBuffer > String

The main performance difference between the String type and the StringBuffer type is that String is an immutable object, so every time you make a change to the String type, it is tantamount to generating a new String object and then pointing the pointer to the new String object. Each generation of objects will have an impact on system performance, especially when there are more non-reference objects in memory, JVM's GC will start to work, which is bound to be quite slow.

If you use the StringBuffer class, the result will be different, each time the result will operate on the StringBuffer object itself, rather than generate a new object, and then change the object reference.

(2) use selection

Scenarios using the String class: you can use the String class in scenarios where strings do not change often, such as the declaration of constants and a small amount of variable operations.

Scenarios using the StringBuffer class: when string operations are performed frequently (such as concatenation, substitution, deletion, etc.) and running in a multithreaded environment, consider using StringBuffer, such as XML parsing, HTTP parameter parsing, and encapsulation.

Scenarios using the StringBuilder class: when string operations are performed frequently (such as concatenation, substitution, and deletion, etc.) and run in a single-threaded environment, you can consider using StringBuilder, such as SQL statement assembly, JSON encapsulation, and so on.

Thank you for your reading, the above is the content of "how to create the String class of Java". After the study of this article, I believe you have a deeper understanding of how to create the String class of Java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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