Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

Java String.formatted() and String.valueOf() methods

Reference for Java String.formatted() (Java 15) and String.valueOf(): format with an instance call and convert any value or char array to a string.

formatted() is the instance version of String.format(): the string you call it on is the template, and the arguments are the values. It was added in Java 15 and works especially well with text blocks, because the template can sit directly in the literal.

String.valueOf() is the opposite kind of tool. It is a family of static methods that turn a primitive, an object or a char[] into a String. It is the null-safe way to ask "give me this as text".

Java
String msg = "Hi %s, you have %d new messages".formatted("Ana", 3);
String text = String.valueOf(42);   // "42"

Try it

Java formatted() with a text block

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java valueOf conversions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public String formatted(Object... args)                    // since 15

public static String valueOf(Object obj)
public static String valueOf(char[] data)
public static String valueOf(char[] data, int offset, int count)
public static String valueOf(boolean b)
public static String valueOf(char c)
public static String valueOf(int i)
public static String valueOf(long l)
public static String valueOf(float f)
public static String valueOf(double d)
Name Description
args Values for the format specifiers in the string. Same rules as String.format().
obj Any object; its toString() is used, or "null" for null.
data, offset, count A character array, and optionally the slice to copy.
other parameters A primitive value to convert.

Returns: a String.

Throws: formatted() can throw the same IllegalFormatException subclasses as String.format(). valueOf(char[]) throws NullPointerException for a null array, and valueOf(char[], int, int) throws IndexOutOfBoundsException for a bad range.

Usage notes

  • formatted() equals String.format(this, args). It uses the default locale; there is no overload that takes a Locale, so use String.format(Locale, ...) when that matters.
  • Text blocks and formatted() read well together, but the whole block is parsed at run time. Keep % characters that should stay literal doubled as %%.
  • valueOf(Object) is null-safe; obj.toString() is not. Be aware that a null becomes the four-letter text "null", not an empty string.
  • The overload is chosen at compile time. String.valueOf((Object) null) prints null, but a null typed as char[] hits the array overload and throws.
  • valueOf(char[]) copies the array. Later changes to the array do not affect the string. String.copyValueOf() does the same thing and exists for historical reasons.
  • Alternatives: Integer.toString(i), "" + i and valueOf(i) give the same text. Prefer the dedicated wrapper methods when you need a radix, see Integer.toString().