Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8203630

String::formatted (Preview)

    XMLWordPrintable

Details

    • CSR
    • Resolution: Approved
    • P3
    • 13
    • core-libs
    • None
    • minimal
    • Java API
    • SE

    Description

      Summary

      Introduce instance versions of the existing String::format method.

      This will be a preview language feature as part of Text Blocks

      Problem

      The static forms of String::format lack a flowiness that is expected in modern Java code. Text Blocks significantly emphasize this condition.

      Example:

              String type = "Integer";
              String variable = "value";
      
              String source = String.format("""
                              public void print(%s %s) {
                                  System.out.println(Objects.toString(object));
                              }
                              """, type, variable);

      In this example, the format is not the focus of the expression, and the argments are near invisible,

      Solution

      Provide instance versions of format methods.

              String type = "Integer";
              String variable = "value";
      
              String source = """
                              public void print(%s %s) {
                                  System.out.println(Objects.toString(object));
                              }
                              """.formatted(type, variable);
      

      Using String instance "format" splits focus more clearly into 1) the format and 2) the arguments.

      Due to method overloading lookup, this solution also requires a unique name other than "format". Hence, the name "formatted".

      Alternatives

      The argument for implementing String::format as static appears to be that the format methods could be imported statically and thus conduct themselves comparably to C's sprintf .

      import static java.lang.String.format;
      ...
              int result = 10;
              String str = format("This result is %d", result);

      However, developers use imported static infrequently and are forced, somewhat redundantly, to add the class selector.

              int result = 10;
              String str = String.format("This result is %d", result);

      Specification

          /**
           * Formats using this string as the format string, and the supplied
           * arguments.
           *
           * @implSpec This method is equivalent to {@code String.format(this, args)}.
           *
           * @param  args
           *         Arguments referenced by the format specifiers in this string.
           *
           * @return  A formatted string
           *
           * @see  java.lang.String#format(String,Object...)
           * @see  java.util.Formatter
           *
           * @since 13
           *
           * @deprecated  This method is associated with text blocks, a preview language feature.
           *              Text blocks and/or this method may be changed or removed in a future release.
           */
          @Deprecated(forRemoval=true, since="13")
          public String formatted(Object... args) {
              return new Formatter().format(this, args).toString();
          }

      Attachments

        Issue Links

          Activity

            People

              jlaskey Jim Laskey
              jlaskey Jim Laskey
              Stuart Marks
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: