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

Popular posts from this blog

html - How to custom Bootstrap grid height? -

javascript - pass values from mssql to views in node -

javascript - Understanding ExtJS Framework Syntax -