Given the 2 toString()
implementations below, which one is preferred:
public String toString(){
return \"{a:\"+ a + \", b:\" + b + \", c: \"
I prefer:
String.format( "{a: %s, b: %s, c: %s}", a, b, c );
...because it's short and readable.
I would not optimize this for speed unless you use it inside a loop with a very high repeat count and have measured the performance difference.
I agree, that if you have to output a lot of parameters, this form can get confusing (like one of the comments say). In this case I'd switch to a more readable form (perhaps using ToStringBuilder of apache-commons - taken from the answer of matt b) and ignore performance again.