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.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-valueof
Java String valueOf() Method - GeeksforGeeks
December 2, 2024 - Explanation: In the above example, the method converts the null object into the text "null" without errors. ... public class ValueOf { public static void main(String[] args) { // Character array char[] ch = { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd' }; String s = String.valueOf(ch, 0, 5); // Convert first 5 characters to string System.out.println("" + s); } }
🌐
W3Schools
w3schools.com › java › ref_string_valueof.asp
Java String valueOf() Method
public static String valueOf(Object data) ❮ String Methods · ★ +1 · Sign in to track progress · REMOVE ADS · Ad-free learning, track your progress, earn XP, streaks, compete in leagues, build and host websites, unlock coding challenges, and much more!
🌐
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).
🌐
DZone
dzone.com › data engineering › data › string.valueof(object) vs. objects.tostring(object)
String.valueOf(Object) Vs. Objects.toString(Object) - DZone
August 28, 2018 - The methods String.valueOf(Object), Objects.toString(Object), and Objects.toString(Object, String) make it easy to provide string representations of objects without the need to write explicit checks for the null. Data Types Strings Object (computer science) Published at DZone with permission of Dustin Marx. See the original article here. Opinions expressed by DZone contributors are their own. Generics in Java and Their Implementation ·
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.
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java string › object.tostring() vs string.valueof()
Object.toString() vs String.valueOf() | Baeldung
April 7, 2025 - The String.valueOf() allows us to safely convert objects to strings without needing to manage null values.
🌐
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. If the Object is null, the return value will be null. ... The java method, String valueOf(Object obj) should be accessed statically thus ...
🌐
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.
🌐
Tutorial Gateway
tutorialgateway.org › java-string-valueof-method
Java String ValueOf Function
September 11, 2025 - public static String valueOf(Object obj); //In order to use in program String.valueOf(Object obj); Return Value: It accepts different types of arguments and returns the String representation of the user-specified argument value.
🌐
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.
🌐
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.
🌐
Medium
medium.com › @AlexanderObregon › javas-string-valueof-method-explained-b3fba964d3ec
Java String.valueOf() Method Explained
July 24, 2024 - Object obj = new Object(); String objString = String.valueOf(obj); // Calls obj.toString() Object nullObj = null; String nullObjString = String.valueOf(nullObj); // "null" The String.valueOf() method in Java is extremely useful in various scenarios where you need to convert different data types into their string representations.
🌐
Baeldung
baeldung.com › home › java › java string › java string.valueof()
Java.String.valueOf() | Baeldung
April 10, 2025 - • Java String.String() • Java ... • Java String.trim() ... The method valueOf() has several overloads that accept one parameter of different types and convert them to a String....
🌐
Tutorialspoint
tutorialspoint.com › java › java_string_valueof.htm
Java - String valueOf() Method
static String valueOf(boolean b) or static String valueOf(char c) or static String valueOf(char[] data) or static String valueOf(char[] data, int offset, int count) or static String valueOf(double d) or static String valueOf(float f) or static String valueOf(int i) or static String valueOf(long l) or static String valueOf(Object obj) ... See the description. This method returns the string representation. import java.io.*; public class Test { public static void main(String args[]) { double d = 102939939.939; boolean b = true; long l = 1232874; char[] arr = {'a', 'b', 'c', 'd', 'e', 'f','g' }; System.out.println("Return Value : " + String.valueOf(d) ); System.out.println("Return Value : " + String.valueOf(b) ); System.out.println("Return Value : " + String.valueOf(l) ); System.out.println("Return Value : " + String.valueOf(arr) ); } }
🌐
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 ...