performance - StringBuilder vs String concatenation in toString() in Java -
given 2 tostring()
implementations below, 1 preferred:
public string tostring(){ return "{a:"+ + ", b:" + b + ", c: " + c +"}"; }
or
public string tostring(){ stringbuilder sb = new stringbuilder(100); return sb.append("{a:").append(a) .append(", b:").append(b) .append(", c:").append(c) .append("}") .tostring(); }
?
more importantly, given have 3 properties might not make difference, @ point switch +
concat stringbuilder
?
version 1 preferable because shorter , the compiler in fact turn version 2 - no performance difference whatsoever.
more importantly given have 3 properties might not make difference, @ point switch concat builder?
at point you're concatenating in loop - that's when compiler can't substitute stringbuilder
itself.
Comments
Post a Comment