The problem is that if we have a lot of string concatenations, we will produce many string objects that will only consume time (for allocation) and memory (until it is garbage collected - removed from memory). If we concatenate "a" + "b", a new string, "ab" is created, and both "a" and "b" will exist for some time.
But, one thing is true - for most cases, specially on a web page, there will be no noticeable performance difference between using a StringBuffer or concatenating the strings directly. It's up to you to determine if a operation is worth using a StringBuffer or not.Here are some examples:
var sb = new StringBuffer(); sb.append("One"); sb.append("Two"); sb.append("Three"); sb.toString() -> "OneTwoThree" sb.length() -> 11 sb.clear(); sb.append("A").append("B").append("C") -> The append returns the StringBuffer itself, so it can be nested
The Map class
|Table of Contents||Next:
Introduction to Parsers