When is StringBuffer/StringBuilder not implicitly used by the compiler?
问题 I've heard that the compiler (or was it the JVM?) will automatically use a StringBuilder for some string concatenation. When is the right time to explicitly declare one? I don't need a StringBuffer for being thread-safe. Thanks. 回答1: The compiler will use it automatically for any string concatenation using "+". You'd usually use it explicitly if you wanted to concatenate in a loop. For example: StringBuilder builder = new StringBuilder(); for (String name : names) { builder.append(name);