1

At https://docs.oracle.com/javase/tutorial/i18n/text/usage.html I found this:

// recommended
System.out.printf("Character %c is invalid.%n", codePoint);

// not recommended
System.out.println("Character " + String.valueOf(char) + " is invalid.");

Why is one recommend and why the other is not?

UPDATE

The arguments provided by the documentation have not much meaning for me excepting the localization one:

This following approach is simple and avoids concatenation, which makes the text more difficult to localize as not all languages insert numeric values into a string in the same order as English.

Adrian
  • 3,321
  • 2
  • 29
  • 46
  • 1
    as it says, it's "difficult to localize". (besides the second example wont even compile, because `char` is a type name, and can't be a variable. I'd recommend you look for some better and more up to date learning resource. Just because Oracle bought Java doesn't mean their website is a good place to learn about it – MightyPork Jun 15 '19 at 11:18

2 Answers2

3
System.out.printf("Character %c is invalid.%n", codePoint); //Recommended

The above approach is recommended because of localization of string is easier in this way. With respect to performance, this approach is not recommended as answered by @oleg.cherednik. This is already answered (more details available here) Is it better practice to use String.format over string Concatenation in Java?

Ashish
  • 997
  • 6
  • 20
2
"Character " + String.valueOf(char) + " is invalid."

JVM transform this to StringBuilder. Bud DO NOT USE + for string IN LOOP. In this case, every when you use +, NEW STRING will be created and put into StringPool.

Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35