Home > front end >  What's the difference between String.format() and str.formatted() in Java?
What's the difference between String.format() and str.formatted() in Java?

Time:02-05

I know that method String.format() is nearly the same as method System.out.printf() except it returns a String. But I could hardly find the introduction about method "formatted" which is defined as follows:

public String formatted(Object... args) {
        return new Formatter().format(this, args).toString();
}

And I know the functions of two codes below are the same.

String str1 = String.format("%s", "abab");
System.out.println(str1);
String str2;
str2 = "%s".formatted("abab");
System.out.println(str2);

Therefore I'm wandering what's the difference between them. Thank you!

CodePudding user response:

The key difference between them is that printf() prints the formatted String into console much like System. out. println() but the format() method returns a formatted string, which you can store or use the way you want.

Another difference is one is declared in java.lang.String class and other is on java.io.PrintStream. You should use String.format() method if you need a formatted String and use System.out.printf() if you need to display formatted String on the console.

Let us understand two key things to learn, first, you can use either printf() or format() to format a String. The key difference between them is that printf() prints the formatted String into console much like System.out.println() but the format() method returns a formatted string, which you can store or use the way you want.

The second most important thing you need to learn for formatting String in Java is formatting instructions like %s, %d, %n etc. The whole formatting process works based on the instruction you give.

// formatting String with dynamic String and Integer input
System.out.printf("length of String %s is %d %n", "abcd", "abcd".length());

This will print "length of String abcd is 4 ", you can see that "abcd" and "4" are dynamically appended into given String based upon formatting instruction and values you passed.

// re-ordering output using explicit argument indices
System.out.printf("%3$2s, %2$2s, %1$2s %n", "Tokyo", "London", "NewYork" );

This will print "NewYork, London, Tokyo", you can see that even though you passed Tokyo as first parameter it appeared last in formatted String because we have re-ordered output using explicitly argument indices.

// formatting Date to String using %D flag, %D is for MM/dd/yy format
System.out.printf("Date in MM/dd/yy format is %tD %n", new Date());

This example will print "Date in MM/dd/yy format is 12/07/16", you can see that today's date is nicely formatted in MM/dd/yy format. This is probably the easiest way to format a date as a String in Java, It doesn't require you to create a SimpleDateFormat object and handle the formatting of the date by yourself.

CodePudding user response:

Make sure you use a good IDE so that you have easy access to browse into JDK source code. In Eclipse say, use F3 to open to any declaration. IntelliJ IDEA has similar feature.

If you view the source code for both methods, you can see these calls are identical except that variables this is interchanged with format when comparing the instance vs static method:

public String formatted(Object... args) {
    return new Formatter().format(this, args).toString();
}
public static String format(String format, Object... args) {
    return new Formatter().format(format, args).toString();
}

So as you've observed: String.format(str, args) is same as str.formatted(args)

  •  Tags:  
  • Related