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
- Public ClassStringCompileTest {
- StringS1 = "This is ";
- StringS2 = "string decompile ";
- StringS3 = "testing .";
- Public VoidStringTest (){
- StringS = s1 + s2 + s3;
- }
- Public VoidStringBufferTest (){
- StringBufferBuffer =New StringBuffer();
- Buffer. append (s1 );
- Buffer. append (s2 );
- Buffer. append (s3 );
- StringS = buffer. toString ();
- }
- }
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