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 optimization methods such as Java string operation, basic operation method, etc.

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

Share

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

This article mainly explains "what is the optimization method of Java string operation, basic operation method and so on", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Java string operation, basic operation methods and other optimization methods are what" it!

String operation optimization

String object

String objects or their equivalent objects (such as char array) always occupy * space blocks in memory, so how to deal with strings efficiently is the key to improve the overall performance of the system.

The String object can be thought of as an extension and further encapsulation of the char array, which is mainly composed of three parts: the char array, the offset, and the length of the String. The char array represents the contents of the String, which is a superset of the strings represented by the String object. The real content of String also needs to be located and intercepted in this char array by offset and length.

String has three basic features:

1. Invariance

two。 Optimization for constant pool

3. The final definition of the class.

Immutability means that once a String object is generated, it can no longer be changed. This feature of String can be generalized to immutable mode, where the state of an object does not change after the object is created. The main function of the invariant pattern is that when an object needs to be shared by multi-threads and accessed frequently, the synchronization and lock waiting time can be omitted, thus greatly improving the system performance.

Optimization for constant pools means that when two String objects have the same value, they only refer to the same copy in the constant pool, and this technique can greatly save memory space when the same string is repeated.

The following code str1, str2, and str4 refers to the same address, but str3 reopens up a piece of memory space. Although str3 takes up the heap space alone, it points to exactly the same entity as str1. The code is shown in listing 1 below.

Listing 1. Sample code

Public class StringDemo {public static void main (String [] args) {String str1= "abc"; String str2 = "abc"; String str3 = new String ("abc"); String str4 = str1; System.out.println ("is str1= str2?" + (str1==str2)); System.out.println ("is str1= str3?" + (str1==str3)); System.out.println ("is str1 refer to str3?" + (str1.intern () = str3.intern (); System.out.println ("is str1= str4" + (str1==str4)) System.out.println ("is str2= str4" + (str2==str4)); System.out.println ("is str4 refer to str3?" + (str4.intern () = = str3.intern ();}}

The output is shown in listing 2.

Listing 2. Output result

Is str1 = str2?true is str1 = str3?false is str1 refer to str3?true is str1 = str4true is str2 = str4true is str4 refer to str3?true

Skills of using SubString

The source code of String's substring method creates a new String object, new String (offset+beginIndex,endIndex-beginIndex,value), on the line * *; the purpose of this line of code is to share char array objects in String efficiently and quickly. But in this method of intercepting strings by offsets, the value array of String's native contents is copied to the new substring. Imagine that if the original string is large and the intercepted character length is short, then the intercepted substring contains all the contents of the original string and occupies the corresponding memory space. and only through the offset and length to determine their own actual value. This algorithm increases speed but wastes space.

The following code demonstrates using the substring method to intercept a very small string in a large string exclusive. If you use string's substring method, it will cause a memory overflow, and if you repeatedly create a new string method, you can ensure normal operation.

Listing 3.substring method demonstration

Import java.util.ArrayList; import java.util.List; public class StringDemo {public static void main (String [] args) {List handler = new ArrayList (); for (int iTuno [)

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