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

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -