According to the Java documentation, String.valueOf() returns:

if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.

So there shouldn't really be a difference except for an additional method invocation.

Also, in the case of Object#toString, if the instance is null, a NullPointerException will be thrown, so arguably, it's less safe.

public static void main(String args[]) {  
    String str = null;
    System.out.println(String.valueOf(str));  // This will print a String equal to "null"        
    System.out.println(str.toString()); // This will throw a NullPointerException
} 
Answer from Kuba Rakoczy on Stack Overflow
🌐
W3Schools
w3schools.com › java › ref_string_valueof.asp
Java String valueOf() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... char[] myArray = {'a', 'b', 'c'}; System.out.println(String.valueOf(myArray)); System.out.println(String.valueOf('A')); System.out.println(String.valueOf(true)); System.out.println(String.valueOf(4.5f)); System.out.println(String.valueOf(5.2)); System.out.println(String.valueOf(12)); System.out.println(String.valueOf(1400L));
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-valueof
Java String valueOf() Method - GeeksforGeeks
December 2, 2024 - Return Type: It returns a String that represents the input data. If the input is null, it returns the text "null". ... public class ValueOf { public static void main(String[] args) { boolean b = true; // Boolean variable String s = ...
🌐
Scaler
scaler.com › home › topics › string valueof() in java
String valueOf() in Java - Scaler Topics
April 8, 2024 - String valueOf() method is present in the String class of java.lang package. valueOf() in Java is used to convert any non-String variable or Object such as int, double, char, and others to a newly created String object.
🌐
TechVidvan
techvidvan.com › tutorials › java-string-valueof-method
Java String valueOf() Method - TechVidvan
November 11, 2024 - Return value It returns the string representation of the supplied argument. ValueOf() This method returns the string’s primitive value. This technique makes no changes to the original string.
Top answer
1 of 11
269

According to the Java documentation, String.valueOf() returns:

if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.

So there shouldn't really be a difference except for an additional method invocation.

Also, in the case of Object#toString, if the instance is null, a NullPointerException will be thrown, so arguably, it's less safe.

public static void main(String args[]) {  
    String str = null;
    System.out.println(String.valueOf(str));  // This will print a String equal to "null"        
    System.out.println(str.toString()); // This will throw a NullPointerException
} 
2 of 11
27

Differences between String.valueOf(Object) and Object.toString() are:

1) If string is null,

String.valueOf(Object) will return "null", whereas Object::toString() will throw a null pointer exception.

public static void main(String args[]){  
    String str = null;
    
    System.out.println(String.valueOf(str));  // it will print null        
    System.out.println(str.toString()); // it will throw NullPointerException        
}  

2) Signature:

valueOf() method of String class is static. whereas toString() method of String class is non static.

The signature or syntax of string's valueOf() method is given below:

public static String valueOf(boolean b)  
public static String valueOf(char c)  
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)  
public static String valueOf(Object o)

The signature or syntax of string's toString() method is given below:

public String toString()
🌐
Codecademy
codecademy.com › docs › java › strings › .valueof()
Java | Strings | .valueOf() | Codecademy
July 26, 2025 - Java’s .valueOf() method of the String class converts various data types like integers, floats, booleans, and objects into their string representations.
🌐
Medium
medium.com › @AlexanderObregon › javas-string-valueof-method-explained-b3fba964d3ec
Java String.valueOf() Method Explained
July 24, 2024 - Java’s String.valueOf() method is a flexible and important tool for converting various data types into a string. This method simplifies the process of data conversion, making it easier to work with different types in a uniform manner. In this article, we will explore the String.valueOf() method, its syntax, typical use cases, and performance implications.
🌐
BeginnersBook
beginnersbook.com › 2017 › 10 › java-string-valueof-method
Java String valueOf() method
June 4, 2024 - public class StringValueOfExample { public static void main(String[] args) { // boolean to String boolean flag = true; String strFlag = String.valueOf(flag); System.out.println("Boolean to String: " + strFlag); // char to String char ch = 'Z'; String strChar = String.valueOf(ch); System.out.println("Char to String: " + strChar); // char array to String char[] chars = {'B', 'o', 'o', 'k'}; String strChars = String.valueOf(chars); System.out.println("Char array to String: " + strChars); // specific portion of char array to String String strPartialChars = String.valueOf(chars, 1, 2); System.out.p
Find elsewhere
🌐
Tutorialspoint
tutorialspoint.com › java › java_string_valueof.htm
Java - String valueOf() Method
Java Vs. C++ ... This method has the following variants, which depend on the passed parameters. This method returns the string representation of the passed argument. valueOf(boolean b) − Returns the string representation of the boolean argument.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
July 21, 2026 - The class String includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by the Character class. The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.string.valueof
String.ValueOf Method (Java.Lang) | Microsoft Learn
Returns the string representation of the long argument. The representation is exactly the one returned by the Long.toString method of one argument. Java documentation for java.lang.String.valueOf(long).
🌐
W3Resource
w3resource.com › java-tutorial › string › string_valueof.php
Java String: valueOf Method - w3resource
August 19, 2022 - public class StringValueOfChar ... value of char c System.out.println(String.valueOf(c)); } } } ... Returns the string representation of the int argument....
🌐
Programiz
programiz.com › java-programming › library › string › valueof
Java String valueOf()
System.out.println(String.valueOf(l)); // "-2343834" System.out.println(String.valueOf(f)); // "23.4" System.out.println(String.valueOf(d)); // "923.234" } } In Java, we can convert char and char array to string using the valueOf() method.
🌐
Javatpoint
javatpoint.com › java-string-valueof
Java String valueOf() - Javatpoint
Java program to find the percentage of uppercase, lowercase, digits and special characters in a string
🌐
Tutorialspoint
tutorialspoint.com › java › lang › string_valueof_object.htm
Java.lang.String.valueOf() Method
The java.lang.String.valueOf(Object obj) method returns the string representation of the Object argument. Following is the declaration for java.lang.String.valueOf() method · public static String valueOf(Object obj) obj − This is an object.
🌐
Baeldung
baeldung.com › home › java › java string › java string.valueof()
Java.String.valueOf() | Baeldung
April 10, 2025 - @Test public void whenCallValueOf_thenCorrect() { long l = 200L; assertEquals("200", String.valueOf(l)); }
🌐
Baeldung
baeldung.com › home › java › java string › object.tostring() vs string.valueof()
Object.toString() vs String.valueOf() | Baeldung
April 7, 2025 - Like most valueOf() methods, it has several overloaded variants to allow it to accept any of these arguments: ... The String.valueOf() implementations do what we would expect. It will return the “true” or “false” string for booleans. We can convert a char or char array into a string.
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › valueOf
Java String valueOf() - Convert To String | Vultr Docs
December 9, 2024 - char[] charArray = {'J', 'a', 'v', 'a'}; String arrayStr = String.valueOf(charArray); System.out.println(arrayStr); // Outputs "Java" Explain Code
🌐
Educative
educative.io › answers › how-to-use-stringvalueof-method-in-java
How to use String.valueOf() method in Java
The String.valueOf() method in Java transforms various forms of data into a string representation. It is an overloaded method that can handle multiple input types such as: bool · char · char[] int · long · float · double · Here’s how ...