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
🌐
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.
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()
🌐
W3Schools
w3schools.com › jsref › jsref_valueof_string.asp
JavaScript String valueOf() Method
The valueOf() method can be used to convert a string object into a string.
🌐
W3Schools
w3schools.com › JAVA › ref_string_valueof.asp
Java String valueOf() Method
The valueOf() method returns the string representation of the specified value.
🌐
Scaler
scaler.com › home › topics › string valueof() in java
String valueOf() in Java - Scaler Topics
April 8, 2024 - The String valueOf() method in Java converts any non-String variable or Object, such as int, double, char, and others, to a newly created string object.
🌐
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. [Android.Runtime.Register("valueOf", "(J)Ljava/lang/String;", "")] public static string ValueOf(long l);
🌐
TechVidvan
techvidvan.com › tutorials › java-string-valueof-method
Java String valueOf() Method - TechVidvan
November 11, 2024 - ValueOf() This method returns the string’s primitive value. This technique makes no changes to the original string. This function helps convert a string object to a string.
Find elsewhere
🌐
DZone
dzone.com › data engineering › data › string.valueof(object) vs. objects.tostring(object)
String.valueOf(Object) Vs. Objects.toString(Object) - DZone
August 28, 2018 - Both methods String.valueOf(Object) and Objects.toString(Object) essentially do the same thing: call the toString() method on a passed-in object. This is the case as long as it's not null or it doesn't return the string "null" when null is passed to them.
🌐
Java Tutorial HQ
javatutorialhq.com › java tutorial › java.lang › string › tolowercase(locale locale) method example
Java String valueOf(Object obj) method example
October 1, 2019 - The method valueOf(Object obj) returns a String object which the equivalent of the toString() of Object.
🌐
Medium
medium.com › @AlexanderObregon › javas-string-valueof-method-explained-b3fba964d3ec
Java String.valueOf() Method Explained
July 24, 2024 - The String.valueOf() method is a static method in the String class, and it is used to convert various primitive data types and objects into their string equivalents. This conversion is particularly useful when you need to concatenate different ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-valueof
Java String valueOf() Method - GeeksforGeeks
December 2, 2024 - public class ValueOf { public static void main(String[] args) { Object o = null; // Null object String s = String.valueOf(o); // Convert object to string System.out.println("" + s); } }
🌐
Baeldung
baeldung.com › home › java › java string › object.tostring() vs string.valueof()
Object.toString() vs String.valueOf() | Baeldung
April 7, 2025 - String.valueOf() is a static method that we can use to turn various data types into strings. Like most valueOf() methods, it has several overloaded variants to allow it to accept any of these arguments: bool or Boolean · Char · Char array ...
🌐
BeginnersBook
beginnersbook.com › 2017 › 10 › java-string-valueof-method
Java String valueOf() method
long l = 123456789L; String strLong = String.valueOf(l); System.out.println(strLong); // Output: "123456789" Converts an Object to a String.
🌐
Baeldung
baeldung.com › home › java › java string › integer.tostring() vs string.valueof() in java
Integer.toString() vs String.valueOf() in Java | Baeldung
April 7, 2025 - Integer.toString() might throw NullPointerException if given Integer object is null. String.valueOf() won’t throw an exception because it’ll go to String.valueOf(Object obj) method and return null.
🌐
Tutorialspoint
tutorialspoint.com › java › java_string_valueof.htm
Java - String valueOf() Method
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.
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › valueOf
Java String valueOf() - Convert To String | Vultr Docs
December 9, 2024 - The valueOf() method in Java's String class is a static utility function designed to convert various data types into their string representations. This method proves invaluable when you need to transform primitive data types, objects, or arrays ...
🌐
Javatpoint
javatpoint.com › java-string-valueof
Java String valueOf() - Javatpoint
Java String valueOf() - The String.valueOf() method in Java is a multipurpose static method. Its major function lies in the conversion of types of data, such as primitive types and objects, into strings. The technique provides an efficient and convenient way to construct string objects from ...
🌐
Programiz
programiz.com › java-programming › library › string › valueof
Java String valueOf()
String.valueOf(boolean b) String.valueOf(char c) String.valueOf(char[] data) String.valueOf(double d) String.valueOf(float f) String.valueOf(int b) String.valueOf(long l) String.valueOf(Object o) Here, valueOf() is a static method. We call the valueof() method using the class name like this: String.valueOf(b); The valueOf() method takes a single parameter.
🌐
InfoWorld
infoworld.com › home › software development
The Value of String.valueOf | InfoWorld
April 19, 2009 - The String.valueOf(Object) method, as its Javadoc-generated documentation states, returns “null” if the passed in object is null and returns the results on the passed-in Object‘s toString() call if the passed-in Object is not null. In other words, String.valueOf(String) does the null checking for you.