new StringBuilder(char)

Michael Kay mike at saxonica.com
Sat Aug 16 22:12:57 UTC 2014


I think the same problem also appears in StringBuffer and has been remarked on repeatedly over the years, e.g.

http://osdir.com/ml/java.findbugs.general/2007-02/msg00068.html


Michael Kay
Saxonica
mike at saxonica.com
+44 (0) 118 946 5893




On 16 Aug 2014, at 20:17, Louis Wasserman <lowasser at google.com> wrote:

> There are indications this bug has actually occurred in released JDK code
> before (!!):
> 
> http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/util/Formatter.java#2882
> 
> To give credit where credit is due, I reported the potential for this issue
> to Eddie after seeing it on StackOverflow at
> http://stackoverflow.com/q/25167015.
> On Aug 16, 2014 5:39 AM, "Andrew Thompson" <lordpixel at me.com> wrote:
> 
>> I've encountered bugs in production code due to this surprise.
>> 
>> A good thing to fix.
>> 
>>> On Aug 16, 2014, at 1:38 AM, Jeremy Manson <jeremymanson at google.com>
>> wrote:
>>> 
>>> No love from core-libs-dev?  It's backwards-incompatible, but in a way
>> that
>>> would unbreak existing broken code.  Might be a worthwhile cleanup.
>>> 
>>> Jeremy
>>> 
>>> 
>>>> On Fri, Aug 8, 2014 at 1:53 PM, Eddie Aftandilian <eaftan at google.com>
>> wrote:
>>>> 
>>>> Hi all,
>>>> 
>>>> We recently realized that calling new StringBuilder(char) does not do
>> what
>>>> you would think it does.  Since there is no char constructor defined,
>> the
>>>> char is widened to an int and the StringBuffer is presized to the
>>>> character's encoded value.  Thus code like this prints an empty string
>>>> rather than the expected "a":
>>>> System.out.println(new StringBuilder('a'));
>>>> 
>>>> Would it be possible to add a char constructor to StringBuilder to
>> prevent
>>>> this problem? I understand this would change the behavior of any code
>> that
>>>> is currently doing this, but it's hard to imagine anyone doing this
>>>> intentionally.  Of the ~20 instances we found in Google's codebase, all
>>>> were bugs.  What is your policy on making changes like this where (a) it
>>>> will cause a change in behavior, but (b) the currently behavior is
>> clearly
>>>> wrong?
>>>> 
>>>> If you're willing to take the change, I'd be happy to send a patch.
>>>> 
>>>> Thanks,
>>>> Eddie
>>>> 
>> 




More information about the core-libs-dev mailing list