Java String valueOf() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In Java, String.valueOf(x) is a static method that returns the text form of almost any value: int, long, double, boolean, char, a char[], or any object.

For an object it returns obj.toString(), or the four-letter text "null" when the reference is null, so it is the safe way to print or log values that may be missing. For a char[] it copies the characters into a new String.

valueOf() is a static method of java.lang.String, available since JDK 1.0. Call it on the class name, as in String.valueOf(...). No import is needed.

Syntax

Call it on the class name and pass the value to convert.

java
String a = String.valueOf(42);         // "42"
String b = String.valueOf(true);       // "true"
String c = String.valueOf(chars, 0, 3);  // first three chars
CallWhat it does
String.valueOf(7)"7".
String.valueOf(0.1 + 0.2)"0.30000000000000004".
String.valueOf('A')"A".
String.valueOf((Object) null)"null".

Numbers use the same text as Integer.toString, Double.toString, and so on, which never depends on the locale: 2.5 always prints with a dot, and large doubles switch to 1.0E7 style.

Library declarations
java
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);

Parameters

ParameterDescription
obj, b, c, i, l, f, dThe value to convert.
dataA char array whose characters are copied.
offset, countFor the range overload: first index and number of chars to copy.

Return Value

Return ValueDescription
StringThe text form of the argument, or "null" for a null object reference.

Java Examples

Example 1 — Convert Primitive Values to Text

One overload per primitive type, all with the same simple call.

java
public class ValueOfPrimitives {
    public static void main(String[] args) {
        System.out.println(String.valueOf(42));
        System.out.println(String.valueOf(2.50));
        System.out.println(String.valueOf(1e7));
        System.out.println(String.valueOf(true));
        System.out.println(String.valueOf('x'));
        System.out.println(String.valueOf(10L).length());
    }
}

How It Works

  1. Overload by type. The compiler chooses valueOf(int), valueOf(double), valueOf(boolean), or valueOf(char) from the argument.
  2. Double formatting rules. 2.50 loses its trailing zero, and ten million switches to scientific notation 1.0E7.
  3. A real String comes back. String.valueOf(10L) is "10", so String methods such as length() work on it and return 2.

Example 2 — Build Strings from a char Array

Convert a whole array and a range, then change the array afterwards.

java
public class ValueOfCharArray {
    public static void main(String[] args) {
        char[] letters = {'J', 'a', 'v', 'a', '!'};

        String all = String.valueOf(letters);
        String some = String.valueOf(letters, 0, 4);
        letters[0] = 'L';

        System.out.println(all);
        System.out.println(some);
        System.out.println(new String(letters));
    }
}

How It Works

  1. Whole array. String.valueOf(letters) copies all five chars into "Java!".
  2. A range by offset and count. valueOf(letters, 0, 4) takes four chars from index 0, leaving out the '!'.
  3. The strings are copies. Changing letters[0] to 'L' affects only the array, which now reads "Lava!".

Example 3 — null Objects Versus null char Arrays

An object reference and a char array behave differently when they are null.

java
public class ValueOfNull {
    public static void main(String[] args) {
        Object nothing = null;
        System.out.println(String.valueOf(nothing));
        System.out.println(String.valueOf((Object) null).length());

        char[] noChars = null;
        try {
            String.valueOf(noChars);
        } catch (NullPointerException e) {
            System.out.println("char[] null: " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. Object overload is null-safe. For a null Object, valueOf returns the text "null" instead of throwing.
  2. It is real text. The returned "null" is a normal four-character string, so length() prints 4.
  3. char[] has no such check. The array overload reads the array immediately, so a null char[] throws NullPointerException.

Frequently Asked Questions

It converts a value of almost any type (primitives, char arrays, objects) into its String form.
Written literally, String.valueOf(null) calls the char[] overload and throws NullPointerException. A null Object variable, or (Object) null, returns "null".
valueOf returns "null" for a null reference, while obj.toString() throws NullPointerException. For non-null objects they give the same text.
Yes. String.valueOf(int) returns Integer.toString(i). Both produce the same digits.
No. The characters are copied, so later changes to the array do not affect the String.

Back to the method index

See all 55 Java String method guides grouped by what they do.

Method index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful