The static content has been hard coded in Java files like this:
String s = "<span class=\"className\">StaticContent</span>";
It has to be externalized using ResourceBundle objects.
A simple method of doing it is:
String s = "<span class=\"className\">" +
ResourceBundleObject.getString("StaticContent.key") + "</span>";
The second method is using StringBuilder object:
StringBuilder sb = new StringBuilder();
sb.append("<span class=\"className\">");
sb.append(ResourceBundleObject.getString("StaticContent.key"));
sb.append("</span>");
String s = sb.toString();
Does second method have advantage over the first one (in terms of resources consumed)?
It’s easier to use first method as it involves very less editing.
In this case, there is no performance advantage of using an explicit
StringBuilder.The JLS states that the Java compiler is allowed to compile a sequence of String concatenation subexpressions into an equivalent creation of a temporary
StringBuilderand sequence ofappendcalls. In this case, the optimization works, and the compiler will generate bytecodes that are effectively the same as the bytecodes you’d get if you usedStringBuilderyourself. In other words, the only net effect of usingStringBuilderis to make your code harder to read.You are only likely to get a performance advantage by using
StringBuilderexplicitly if your code does concatenation in a loop. However, if you build long strings this way, the performance difference can be significant.