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
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()
🌐
Baeldung
baeldung.com › home › java › java string › object.tostring() vs string.valueof()
Object.toString() vs String.valueOf() | Baeldung
April 7, 2025 - When we pass an object to String.valueOf(), it will call the toString() method on that object and return the output. The toString() method allows us to customize the output when we want to convert an object to a string.
🌐
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 ...
🌐
Code4reference
code4reference.com
which one is better valueOf() or toString() in Java – Code for Reference
String.valueOf() has more readability. If you have an array of Object class containing Float, Integer, Char etc. First you have to identify all the object then type-cast to appropriate class and call toString() method. Which is kind of inefficient. If valueOf() method is called then it matches ...
🌐
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 - There is one extra call in the stack trace when we use String.valueOf() method because it internally uses the same Integer.toString() method.
🌐
DEV Community
dev.to › tapuchandramojumder › understanding-valueof-and-tostring-in-javascript-7ph
Understanding valueOf() and toString() in JavaScript - DEV Community
April 21, 2024 - In these examples, we have objects obj1 and obj2. obj1 defines a custom valueOf() method that returns the numeric value 40. JavaScript automatically invokes the valueOf() method, resulting in the number 42. obj2 defines a custom toString() method that returns a string representation of the person's name and age.
🌐
Electro4u
electro4u.net › blog › comparing-integertostring-and-stringvalueof-in-java-1417
Comparing Integer.toString() and String.valueOf() in Java
This is because the Integer.toString() method is optimized for the specific task of converting integers to strings, whereas the String.valueOf() method is used for the general purpose of converting any type of object to a string.
🌐
TutorialsPoint
tutorialspoint.com › what-is-the-difference-between-string-valueof-and-tostring-in-java
What is the difference between String.valueOf() and toString() in Java?
import java.io.*; public class Test { public static void main(String args[]) { String Str = new String("Welcome to Tutorialspoint.com"); System.out.print("Return Value :"); System.out.println(Str.toString()); } } ... The value of method variants of the String class accepts different variables such as integer, float, boolean etc.. and converts them into String. ... public class Sample { public static void main(String args[]){ int i = 200; float f = 12.0f; char c = 's'; char[] ch = {'h', 'e', 'l', 'l', 'o'}; String data = String.valueOf(i); System.out.println(String.valueOf(i)); System.out.println(String.valueOf(f)); System.out.println(String.valueOf(c)); System.out.println(String.valueOf(ch)); } }
Find elsewhere
🌐
javakeypoint
javakeypoint.wordpress.com › 2018 › 01 › 20 › differences-between-string-valueofobject-and-object-tostring
Differences between String.valueOf(Object) and Object.toString() | javakeypoint
January 21, 2018 - Java String valueOf method String class valueOf(Object) method converts different types of values into string. valueOf() method of String class is static Using String .valueOf(Object) method, you can convert int to string(public static String valueOf(int i) ) long to string(public static String valueOf(lang i) ) character to string(public static String valueOf(char c)) float to string(public static…
🌐
JavaBeat
javabeat.net › home › what is the difference between tostring() and valueof() in java
What is the Difference Between toString() and valueOf() in Java
July 31, 2023 - The difference between both the methods is that the “toString()” method throws an “NullPointerException” if the passed value is null. Also, it cannot be applied on the primitive data types like “int”, “char”, etc.
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript tutorial › javascript valueof vs. tostring methods
JavaScript valueOf vs. toString Methods
November 14, 2024 - The toString() method will return ... JavaScript (javascript) For most objects, the valueOf() returns the objects themselves whereas the toString() returns the [object Object]. For example: const person = { name: 'John Doe', ...
🌐
Quora
quora.com › What-is-the-difference-between-String-valueOf-and-object-toString-in-Java
What is the difference between String.valueOf() and object.toString() in Java? - Quora
Answer (1 of 3): There is no big difference between two method except one is less safer than the other. in object.toString() if the instance of an object is null it will throw a null pointer exception so it is less safer. String.valueOf() will print a null. For example: String str = null; Sy...
🌐
Oracle
forums.oracle.com › ords › apexds › post › exact-difference-and-usage-between-tostring-string-valueof-7699
Exact Difference and Usage between .toString / String.valueOf() / (String) - Oracle Forums
April 27, 2010 - Hi What are the differences in .toString / String.valueOf() / (String) ? I have tried the following : String a = "a"; Object o = "o"; int i = 1; System.out....
🌐
Coderanch
coderanch.com › t › 508160 › java › Difference-valueOf-Method-toString-Method
Difference between valueOf()Method and toString()Method (Beginning Java forum at Coderanch)
Arun raghvan wrote:Both are used to Convert parameter and give it in String Format . So what is the difference No. Only the String.valueOf method is used to get the String represenation of it's parameter object. The toString method (which all classes have) does not take a parameter.
🌐
Codemia
codemia.io › knowledge-hub › path › stringvalueof_vs_objecttostring
String.valueOf vs. Object.toString
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
Medium
medium.com › @AlexanderObregon › javas-string-valueof-method-explained-b3fba964d3ec
Java String.valueOf() Method Explained
July 24, 2024 - Here, an object and a null object are converted to strings and logged. For the object, String.valueOf() calls its toString() method, while for the null object, it returns the string "null". This makes sure that even null values are safely converted and logged.
🌐
Java by examples
javaquery.com › 2015 › 06 › what-is-difference-between.html
Java by examples: What is the difference between String.valueOf() and toString() in Java?
June 1, 2015 - /* Original Source code */ public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toString(); } toString() toString() can cause the java.lang.NullPointerException. It throws java.lang.NullPointerException when Object is null and also terminate the execution of program in case its not handled properly.
🌐
javakeypoint
javakeypoint.wordpress.com › 2019 › 06 › 12 › difference-between-integer-tostring-and-string-valueof
Difference between integer.tostring and string.valueof | javakeypoint
June 12, 2019 - string.valueof () method return "null" String if pass value is null. integer.tostring() method will be throw NullPointerException if pass value is null. Example:- public class Test { public static void main(String args[]) { String str = null; System.out.println(str.toString()); // This will throw a NullPointerException System.out.println(String.valueOf(str)); // This will print "null" } }
🌐
Sololearn
sololearn.com › en › Discuss › 1492180 › is-tostring-equals-to-string-valueof
Is .toString() equals to String.valueOf()?
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.