The story idea of this article comes from a post “a line of code caused by the murder” on Maimai.
In fact, the article about string, I also wrote a weird string problem before, string for our developers, can use a very popular phrase recently “use good haiyo, as if life reached the peak”.
It is true that everyone uses it very well, and it is very convenient, but how many people really care about the efforts of the JDK engineers behind it?
Let’s use an example to illustrate this in detail.
Public class StringTest {public static void main(String[] args) {// No variable String concatenation String s ="aa"+"bb"+"dd"; System.out.println(s); // Variable String concatenation String g ="11"+s+5; System.out.println(g); // Use String concatenation String a = in the loop"0";
for(int i = 1; i < 10; i++) { a = a + i; } System.out.println(a); StringBuilder StringBuilder b = new StringBuilder();for(int i = 1; i < 10; i++) { b.append(i); } System.out.println(b); }}Copy the code
Some of you might say, well, this is a piece of code, how do you tell the difference? Since I said in the conversation that Java is optimized at compile time starting with JDK5, what does the javac command do at compile time? The bottom line is that the.java source code must be compiled into.class files, which are often referred to as intermediate language — bytecode. The JVM engine then validates the.class file, parses it, and translates the executable machine instructions. This is just the simplest model. In fact, modern JVM engines will do many optimizations later, such as code hotspot analysis, JIT compilation, escape analysis, and so on.
Speaking of which, I remember that some students in the group said that bytecode was so ugly that they did not want to look at it again after seeing it at first sight. Haha, ugly is ugly, but it is full of connotation, powerful and cross-platform.
As for how to view Bytecode, I have shared two tools, one that comes with the JDK, javap, and one that comes with IDEA, Jclasslib Bytecode Viewer. Today, I will share another tool I used to crack APK, JAD, it will make you look at bytecode files much easier.
For a start, I decompiled the JVM instructions using Jdk 1.6-1.8 and found that they were the same, so I won’t list the instruction files generated for each version here. In order to make it easy for you to read the command file, the jad tool is used to generate the following code.
Compiled by Jad V1.5.8g. Copyright 2001 Pavel Kouznetsov. // Jad home Page: decomcompiled by Jad V1.5.8g. Copyright 2001 Pavel Kouznetsov. http://www.kpdus.com/jad.html // Decompiler options: packimports(3) annotate // Source File Name: StringTest.java import java.io.PrintStream; public class StringTest { publicStringTest()
{
// 0 0:aload_0
// 1 1:invokespecial #1
/ / 2. 4:return
}
public static void main(String args[])
{
String s = "aabbdd";
// 0 0:ldc1 #2
// 1 2:astore_1
System.out.println(s);
// 2 3:getstatic #3
// 3 6:aload_1
// 4 7:invokevirtual #4
String g = (new StringBuilder()).append("11").append(s).append(5).toString();
// 5 10:new #5
// 6 13:dup
// 7 14:invokespecial #6
// 8 17:ldc1 #7
// 9 19:invokevirtual #8
// 10 22:aload_1
// 11 23:invokevirtual #8
// 12 26:iconst_5
// 13 27:invokevirtual #9
// 14 30:invokevirtual #10
// 15 33:astore_2
System.out.println(g);
// 16 34:getstatic #3
// 17 37:aload_2
// 18 38:invokevirtual #4
String a = "0";
// 19 41:ldc1 #11
// 20 43:astore_3
for(int i = 1; i < 10; i++)
//* 21 44:iconst_1
//* 22 45:istore 4
//* 23 47:iload 4
//* 24 49:bipush 10
//* 25 51:icmpge 80
a = (new StringBuilder()).append(a).append(i).toString();
// 26 54:new #5
// 27 57:dup
// 28 58:invokespecial #6
// 29 61:aload_3
// 30 62:invokevirtual #8
// 31 65:iload 4
// 32 67:invokevirtual #9
// 33 70:invokevirtual #10
// 34 73:astore_3
// 35 74:iinc 4 1
//* 36 77:goto 47
System.out.println(a);
// 37 80:getstatic #3
// 38 83:aload_3
// 39 84:invokevirtual #4
StringBuilder b = new StringBuilder();
// 40 87:new #5
// 41 90:dup
// 42 91:invokespecial #6
// 43 94:astore 4
for(int i = 1; i < 10; i++)
//* 44 96:iconst_1
//* 45 97:istore 5
//* 46 99:iload 5
//* 47 101:bipush 10
//* 48 103:icmpge 120
b.append(i);
// 49 106:aload 4
// 50 108:iload 5
// 51 110:invokevirtual #9
// 52 113:pop
// 53 114:iinc 5 1
//* 54 117:goto 99
System.out.println(b);
// 55 120:getstatic #3
// 56 123:aload 4
// 57 125:invokevirtual #12
/ / 58, 128:return}}Copy the code
Here are the results of the analysis.
1, variable free string concatenation, values are determined at compile time, so javac tools help us compile it directly into a character constant.
String concatenation with variables. The value of the variable cannot be determined at compile time, so a StringBuilder object is generated at run time.
3. String concatenation is used in the loop. Within the loop, a new StringBuilder object is generated every time the loop is looped.
4. Using StringBuilder outside the loop and append() to concatenate strings inside the loop will only create a StringBuilder object.
Therefore, it is recommended to use StringBuilder and StringBuffer for looping string concatenation operations to improve performance.
In fact, the above conclusion, “Alibaba Java development manual” has been mentioned, this article just corresponds to the conclusion.
A simple string, it is really simple to use, behind the efforts of many engineers, here, deeply admire Zhan Ye.
PS: This article was originally published on wechat public account “Java interview those things”, follow and reply to “1024”, free access to learning materials. The original address