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 String and StringBuffer in java

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

Share

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

This article mainly shows you "how to use String and StringBuffer in java". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to use String and StringBuffer in java".

The JAVA language provides operations on variables of type String. However, if it is not used properly, it will affect the performance of the program. Such as the following statement:

String name=new String ("HuangWeiFeng")

System.out.println (name+ "is my name")

It seems to be very concise, but in fact it is not. To generate binary code, perform the following steps and operations:

(1) generate a new string new String (STR_1)

(2) copy the string

(3) load the string constant "HuangWeiFeng" (STR_2)

(4) the Constructor that invokes the string

(5) Save the string to the array (starting with position 0)

(6) get static out variables from java.io.PrintStream class

(7) generate a new string buffer variable new StringBuffer (STR_BUF_1)

(8) copy the string buffer variable

(9) A Constructor that invokes string buffering

(10) Save the string to the array (starting at position 1)

(11) call the append method in the string buffer (StringBuffer) class with STR_1 as the parameter

(12) load the string constant "is my name" (STR_3)

(13) call the append method in the string buffer (StringBuffer) class with STR_3 as the parameter

(14) execute the toString command for STR_BUF_1

(15) call the println method in the out variable to output the result.

As you can see, these two simple lines of code generate five object variables, STR_1,STR_2,STR_3,STR_4 and STR_BUF_ 1. Instances of these generated classes are typically stored in the heap. The heap initializes the superclasses and instances of all classes, while calling the class and the constructor of each superclass. These operations consume system resources very much. Therefore, it is absolutely necessary to restrict the generation of objects.

After modification, the above code can be replaced with the following code.

StringBuffer name=new StringBuffer ("HuangWeiFeng")

System.out.println (name.append ("is my name."). ToString ()

The system will do the following:

(1) generate a new string buffer variable new StringBuffer (STR_BUF_1)

(2) copy the string buffer variable

(3) load the string constant "HuangWeiFeng" (STR_1)

(4) A Constructor that invokes string buffering

(5) Save the string to the array (starting with position 1)

(6) get static out variables from java.io.PrintStream class

(7) load STR_BUF_1

(8) load the string constant "is my name" (STR_2)

(9) call the append method in the string buffer (StringBuffer) instance with STR_2 as the parameter

(10) execute the toString command (STR_3) for STR_BUF_1

(11) call the println method in the out variable to output the result.

From this, we can see that the improved code only generates four object variables: STR_1,STR_2,STR_3 and STR_BUF_1. You may think that generating one less object will not greatly improve the performance of the program. But snippet 2 below will execute twice as fast as snippet 1. Because snippet 1 generates eight objects, while snippet 2 generates only four objects.

Code snippet 1:

String name= new StringBuffer ("HuangWeiFeng")

Name+= "is my"

Name+= "name"

Code snippet 2:

StringBuffer name=new StringBuffer ("HuangWeiFeng")

Name.append ("is my")

Name.append ("name.") toString ()

The above is all the contents of the article "how to use String and StringBuffer 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

Development

Wechat

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

12
Report