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

2013-02-27 Thread Jan Ohlsen
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) : "

Re: Why is StringBuilder.toString() returning new String instances for empty strings ?

2013-02-28 Thread Volker Simonis
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,

Re: Why is StringBuilder.toString() returning new String instances for empty strings ?

2013-02-28 Thread Remi Forax
On 02/28/2013 09:07 AM, Volker Simonis wrote: 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..". R

Re: Why is StringBuilder.toString() returning new String instances for empty strings ?

2013-02-28 Thread Jan Ohlsen
Yes, makes sense to avoid the branching in that method. Thanks. On Thu, Feb 28, 2013 at 10:23 AM, Remi Forax wrote: > On 02/28/2013 09:07 AM, Volker Simonis wrote: > >> Probably because the API-Spec >> (http://docs.oracle.com/**javase/7/docs/api/java/lang/** >> StringBuilder.html#toString%**28