RFR - JDK-8203444 String::formatted (Preview)

Florian Weimer fweimer at redhat.com
Wed May 22 08:21:54 UTC 2019


* Jim Laskey:

> Good point. To make sure I fully understand what you are stating,
>
> - The argument for having an instance method is reasonable to achieve
> "flowiness".

Right.

> - However, only one version is necessary or desired, that is, "public
> String formatted(Object... args)".

Yes, this appears to be the common use case to me.  I doubt the overload
with its ambiguity is sufficiently widely used to be worth the potential
confusion it can create.  Adding the method with the Locale parameter
under a different name would be okay (except that I'm not sure how
widely it is going to be used).

My totally random set of Java classes (mostly from Fedora and
downstream) shows this:


SELECT jr.descriptor, COUNT(*)  descriptor
  FROM symboldb.java_reference jr
  WHERE jr.class_name = 'java/lang/String' AND jr.name = 'format'
  GROUP BY jr.descriptor;
  
                                 descriptor                                  | descriptor 
-----------------------------------------------------------------------------+------------
 (Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;                   |       9220
 (Ljava/util/Locale;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String; |        436
(2 rows)

So for the existing String::format method, the version without the
explicit Locale argument is much more commonly used.

> - In cases where Locale needs to be overridden, then either use
> "String.format(Locale lc, String fmt, Object... args)" or globally set
> "Locale.setDefault(Locale lc)".

I wouldn't recommend changing global state in this way.

Thanks,
Florian


More information about the core-libs-dev mailing list