Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Java reference

String.valueOf()

Converts a value of any type to a string.

Available since: Java 1.0

Syntax

static String valueOf(Object obj)

Parameters

ParameterTypeDescription
objObjectA number, a char, a boolean, a char[] array or any object.

Return value

StringThe string form of the value. For null it is the string null.

Examples

Numbers and other values to a string:

void main() {
    String a = String.valueOf(42);
    String b = String.valueOf(3.5);
    String c = String.valueOf(true);
    IO.println(a + b + c);
    IO.println(a.length());
}

Output:

423.5true
2

Unlike toString(), it does not fail on null:

void main() {
    Object value = null;
    IO.println(String.valueOf(value));
}

Output:

null

Notes

The reverse conversion is Integer.parseInt(), Double.parseDouble(). A shortcut to turn a number into a string is adding an empty string: "" + 42, but String.valueOf() states the intent more clearly.

See also

Related recipes

In the tutorial

  • Type conversion — Automatic widening, explicit (int) casts, turning strings into numbers with Integer.parseInt and numbers into strings.

Thanks for reading!

Donate to the site — we work hard for you.