java - Does String concatenation get optimized to use existing StringBuilders? -
i have following code:
stringbuilder str = new stringbuilder("foo"); for(field f : fields){ str.append("|" + f); } str.append("|" + bar); string result = str.tostring(); i know compile optimize string concatenation "|" + f , replace stringbuilder. new stringbuilder created or existing str used in java 8? how java 9?
by default in java-9 there no stringbuilder string concatenation - runtime decision how it's made via invokedynamic. , default policy is not stringbuilder::append one.
you can read more here.
under java-8 new 1 created (really easy spot 2 occurrences of invokespecial // method java/lang/stringbuilder."<init>":()v in de-compiled bytecode.
also, have suggestion append.append...; notice better sb.append ... sb.append, , here why.
Comments
Post a Comment