RFR(S): 8067471: Use private static final char for empty Strings
lev.priima at oracle.com
Fri Dec 19 01:42:33 UTC 2014
Sure this examples will not be affected, but we have to concern about
other applications which are probably using this constructor. Could you
suggest such applications?
On 17.12.2014 21:49, Remi Forax wrote:
> On 12/17/2014 11:22 AM, Lev Priima wrote:
>> Please review space optimization in no args String constructor.
>> Originally, it was already rejected once by suggestion in
>> w/o formal justification of pros and contras. And enhancement was
>> requested again by Nathan Reynolds.
>> Issue: https://bugs.openjdk.java.net/browse/JDK-8067471
>> Patch: http://cr.openjdk.java.net/~lpriima/8067471/webrev.00/
> Hi Lev,
> new String() is usually used by beginners that forget or doesn't know
> that you they can use "" instead.
> There is only one legit usage when you want a NullObject for a String
> *and* you don't want to have a shared reference (something visible by
One legit usage of which code?
> You find 4 occurences of new String() in the JDK,
> > jdk.httpserver/share/classes/sun/net/httpserver/Request.java:200: v
> = new String();
> > java.base/share/classes/sun/net/www/MessageHeader.java:508: v = new
> MT_newString = methodType(void.class); //()V for new String()
> String stringEntry = new String();
> For the first two, the code is very similar and "" can be used instead
> of new String().
> The third occurence is just a comment.
just comment to review all MH libs (e.g. nashorn) for such kind of code:
import static java.lang.invoke.MethodHandles.*;
import static java.lang.invoke.MethodType.*;
and more implicit variations.
> The last occurence is typically a code written by a beginner, the code
> use String.concat, do a kind of defensive copy on String (line 967).
> Again, "" can be used instead of new String().
> so my suggestion is to wait to have more information from Nathan
> Reynolds before trying to fix something,
> new String() is not the only way to have a String with an empty char
More information about the core-libs-dev