Java knowledge: String and StringBuffer

Source: Internet
Author: User
Java knowledge: For String and StringBuffer-general Linux technology-Linux programming and kernel information, see the following for details. The JAVA language provides operations for String-type variables. However, improper use may affect the program performance. The following statement:

String name = new String ("HuangWeiFeng ");
System. out. println (name + "is my name ");

It seems to have been quite streamlined, but not actually. To generate binary code, perform the following steps and operations:

(1) generate a new String (STR_1 );
(2) copy the string;
(3) load the String constant "HuangWeiFeng" (STR_2 );
(4) Constructor );
(5) Save the string to the array (starting from position 0 );
(6) obtain the static out variable from the java. io. PrintStream class;
(7) generate a new string buffer variable new StringBuffer (STR_BUF_1 );
(8) copy the buffer variable of the string;
(9) Call the Constructor );
(10) Save the string to the array (starting from position 1 );
(11) using STR_1 as the parameter, call the append method in the string buffer class;
(12) load the String constant "is my name" (STR_3 );
(13) using STR_3 as the parameter, call the append method in the string buffer class;
(14) execute the toString command for STR_BUF_1;
(15) Call the println method in the out variable to output the result.

The two simple lines of code generate five object variables STR_1, STR_2, STR_3, STR_4, and STR_BUF_1. The instances of these generated classes are generally stored in the heap. The heap needs to initialize the super classes and class instances of all classes, and also call the framework of every super class of the class. These operations consume a lot of system resources. Therefore, it is necessary to limit 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 perform the following operations:

(1) generate a new string buffer variable new StringBuffer (STR_BUF_1 );
(2) copy the buffer variable of this string;
(3) load the String constant "HuangWeiFeng" (STR_1 );
(4) Call the Constructor );
(5) Save the string to the array (starting from position 1 );
(6) obtain the static out variable from the java. io. PrintStream class;
(7) Load STR_BUF_1;
(8) load the String constant "is my name" (STR_2 );
(9) using STR_2 as the parameter, call the append method in the string buffer instance;
(10) execute the toString command (STR_3) for STR_BUF_1 );
(11) Call the println method in the out variable to output the result.

It can be seen that the improved code only generates four object variables: STR_1, STR_2, STR_3 and STR_BUF_1. you may think that generating less than one object will not greatly improve the program performance. However, the execution speed of code segment 2 below will be twice that of code segment 1. Because code segment 1 generates eight objects, while code segment 2 generates only four objects.

Code Segment 1:

String name = new StringBuffer ("HuangWeiFeng ");
Name + = "is my ";
Name + = "name ";

Code Segment 2:

StringBuffer name = new StringBuffer ("HuangWeiFeng ");
Name. append ("is my ");
Name. append ("name."). toString ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.