Do String Join Operations produce more objects than StringBuffer?

Source: Internet
Author: User

I wrote an article the day before yesterday. Java performance trap: is the performance of StringBuffer really better than that of String ?, Two netizens commented and saw three negative votes. Therefore, they felt that it was necessary to conduct further explanation and research. For the problem that the direct connection to strings mentioned in the comments of netizens will produce more junk objects, think of the article's source article in parallel (using String or StringBuffer? (Cherami) mentioned in the decompilation class file skills, I feel you can verify it, so I did the following test.

Step 1: Prepare
To facilitate subsequent testing, it is necessary to write a simple script:
Echo test by jdk1.2.2
/Opt/java/jdk1.2.2/bin/javac StringCompileTest. java
/Opt/java/jdk1.2.2/bin/javap-c StringCompileTest
Echo test by jdk1.3.1 _ 09
/Opt/java/jdk1.3.1 _ 09/bin/javac StringCompileTest. java
/Opt/java/jdk1.3.1 _ 09/bin/javap-c StringCompileTest
Echo test by jdk1.4.2
/Opt/java/jdk1.4.2/bin/javac StringCompileTest. java
/Opt/java/jdk1.4.2/bin/javap-c StringCompileTest

The above script can be applied on windows or linux as needed. I tested it in linux, so I saved it as a file stringdecompile. sh. If you test it on windows, you can save it as stringdecompile. bat.

Step 2: Test the code

  1. Public ClassStringCompileTest {
  2. StringS1 = "This is ";
  3. StringS2 = "string decompile ";
  4. StringS3 = "testing .";
  5. Public VoidStringTest (){
  6. StringS = s1 + s2 + s3;
  7. }
  8. Public VoidStringBufferTest (){
  9. StringBufferBuffer =New StringBuffer();
  10. Buffer. append (s1 );
  11. Buffer. append (s2 );
  12. Buffer. append (s3 );
  13. StringS = buffer. toString ();
  14. }
  15. }

Running result:
Test by jdk1.2.2
Compiled from StringCompileTest. java
Public class StringCompileTest extends java. lang. Object {
Java. lang. String s1;
Java. lang. String s2;
Java. lang. String s3;
Public StringCompileTest ();
Public void stringBufferTest ();
Public void stringTest ();
}
 
Method StringCompileTest ()
0 aload_0
1 invokespecial #8 <Method java. lang. Object ()>
4 aload_0
5 ldc #1 <String "This is a">
7 putfield #12 <Field java. lang. String s1>
10 aload_0
11 ldc #2 <String "string decompile">
13 putfield #13 <Field java. lang. String s2>
16 aload_0
17 ldc #3 <String "testing.">
19 putfield #14 <Field java. lang. String s3>
22 return
 
Method void stringBufferTest ()
0 new #7 <Class java. lang. StringBuffer>
3 dup
4 invokespecial #9 <Method java. lang. StringBuffer ()>
7 astore_1
8 aload_1
9 aload_0
10 getfield #12 <Field java. lang. String s1>
13 invokevirtual #11 <Method java. lang. StringBuffer append (java. lang. String)>
16 pop
17 aload_1
18 aload_0
19 getfield #13 <Field java. lang. String s2>
22 invokevirtual #11 <Method java. lang. StringBuffer append (java. lang. String)>
25 pop
26 aload_1
27 aload_0
28 getfield #14 <Field java. lang. String s3>
31 invokevirtual #11 <Method java. lang. StringBuffer append (java. lang. String)>
34 pop
35 aload_1
36 invokevirtual #15 <Method java. lang. String toString ()>
39 astore_2
40 return
 
Method void stringTest ()
0 new #7 <Class java. lang. StringBuffer>
3 dup
4 aload_0
5 getfield #12 <Field java. lang. String s1>
8 invokestatic #16 <Method java. lang. String valueOf (java. lang. Object)>
11 invokespecial #10 <Method java. lang. StringBuffer (java. lang. String)>
14 aload_0
15 getfield #13 <Field java. lang. String s2>
18 invokevirtual #11 <Method java. lang. StringBuffer append (java. lang. String)>
21 aload_0
22 getfield #14 <Field java. lang. String s3>
25 invokevirtual #11 <Method java. lang. StringBuffer append (java. lang. String)>
28 invokevirtual #15 <Method java. lang. String toString ()>
31 astore_1
32 return
Test by jdk1.3.1 _ 09
Compiled from StringCompileTest. java
Public class StringCompileTest extends java. lang. Object {
Java. lang. String s1;
Java. lang. String s2;
Java. lang. String s3;
Public StringCompileTest ();
Public void stringTest ();
Public void stringBufferTest ();
}
 
Method StringCompil

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.