Why is StringBuilder.toString() returning new String instances for empty strings ? (original) (raw)

Volker Simonis volker.simonis at gmail.com
Thu Feb 28 08:07:09 UTC 2013


Probably because the API-Spec (http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html#toString%28%29) says "..A new String object is allocated and initialized to contain the character sequence currently represented by this object..".

Regards, Volker

On Thu, Feb 28, 2013 at 8:47 AM, Jan Ohlsen <jan.t.ohlsen at gmail.com> wrote:

System.out.println( new StringBuilder().toString() == "" ); -> "false"

Any reason for not returning the "" instance? public String toString() { return new String(value, 0, count); } --> public String toString() { return (count > 0) ? new String(value, 0, count) : ""; } ?



More information about the core-libs-dev mailing list