Java Compiler replacing StringBuilder with + concatenation
问题 Here's some simple Java code: String s = new StringBuilder().append("a").append("b").append("c").toString(); I compile it with JRE 1.6, and I observe the following in the decompiled class file: String s = "a" + "b" + "c"; I had the following questions: Why does the compiler choose '+' over StringBuilder? Do we have any official Java specification that justifies this behavior? Does it really make sense to use StringBuilder in such cases, where we know compiler is going to change it anyway? 回答1