From source code:
public String toJson(Object src, Type typeOfSrc) {
StringWriter writer = new StringWriter();
this.toJson((JsonElement)this.toJsonTree(src, typeOfSrc), (Appendable)writer);
return writer.toString();
}
StringWriter uses StringBuffer internally; why not use StringBuilder for better performance???
StringWriteris a Writer, it is nothing likeStringBufferand the purpose of each is so far from the other that it would be easier to explain the similarities which would be relegated to similarities that exist between all Objects. You should use aStringWriterwhen you want aWriter(a character stream). You should use aStringBufferwhen you need a mutable buffer for constructing Strings, or must construct a String in such a way that it cannot be done using String's constructors.They use it because they need a character stream.