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.valueO...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › valueOf
String.prototype.valueOf() - JavaScript - MDN Web Docs
const stringObj = new String("foo"); console.log(stringObj); // Expected output: String { "foo" } console.log(stringObj.valueOf()); // Expected output: "foo" ... A string representing the primitive value of a given String 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()
🌐
Medium
medium.com › @AlexanderObregon › javas-string-valueof-method-explained-b3fba964d3ec
Java String.valueOf() Method Explained
July 24, 2024 - StringBuilder sb = new StringBuilder(); for (int i = 0; i < 1000; i++) { sb.append(String.valueOf(i)).append(" "); } String result = sb.toString(); Reuse Objects: Where possible, reuse objects and avoid unnecessary conversions.
🌐
Scaler
scaler.com › home › topics › string valueof() in java
String valueOf() in Java - Scaler Topics
April 8, 2024 - ... Scaler learners achieved 2.5x ... want to concatenate non-string variables. For example, consider a function that gives us the version of a file in x.y format where x is the major version and y is the minor version....
🌐
Tutorialspoint
tutorialspoint.com › java › lang › string_valueof_object.htm
Java.lang.String.valueOf() Method
package com.tutorialspoint; import ... str; // returns the string representation of the Object argument System.out.println("Value = " + str.valueOf(objVal)); } }...
🌐
DZone
dzone.com › data engineering › data › string.valueof(object) vs. objects.tostring(object)
String.valueOf(Object) Vs. Objects.toString(Object) - DZone
August 28, 2018 - Although I typically use ... for example, one could use this method to return an empty string (""), and the string "nil", the string "none", or any other arbitrary string if a passed-in object was null....
🌐
W3Schools
w3schools.com › jsref › jsref_valueof_string.asp
JavaScript String valueOf() Method
More examples below. The valueOf() method returns the primitive value of a string. The valueOf() method does not change the original string. The valueOf() method can be used to convert a string object into a string. The toString() Method · The valueOf() method is the default method for JavaScript strings.
Find elsewhere
🌐
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(exampleCharArray)); assertEquals("123.935", String.valueOf(123.935)); assertEquals("2222.3", String.valueOf(2222.3f)); assertEquals("2222", String.valueOf(2222)); assertEquals("123456789", String.valueOf(123456789L)); assertEquals("123456789", String.valueOf(123456789L)); assertEquals("Student(Alice, age 5)", ...
🌐
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);
🌐
BeginnersBook
beginnersbook.com › 2017 › 10 › java-string-valueof-method
Java String valueOf() method
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
🌐
TechVidvan
techvidvan.com › tutorials › java-string-valueof-method
Java String valueOf() Method - TechVidvan
November 11, 2024 - 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)
🌐
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 - We have used the Employee class to demonstrate how to override the toString() method of Object class which our Employee class inherited automatically. ... package com.javatutorialhq.java.tutorial.string; import com.teknoscope.tutorial.utilities.Employee; /* * Example source code to show the use of static method valueOf(Object obj) * We have used the Employee class to serve as our Object * The employee class has overriden method of toString() */ public class StringValueOfDemo { public static void main(String[] args) { // Instantiate a new Employee class Employee emp = new Employee("Will Smith|30|January 12, 1983|Single|USA"); // Getting the toString() value of Employee class String objString = String.valueOf(emp); // Printing the String equivalent of Employee class System.out.println(objString); } }
🌐
Programiz
programiz.com › java-programming › library › string › valueof
Java String valueOf()
// Output: "[Java, Python, Kotlin]" result = String.valueOf(languages); System.out.println(result); } } Here, an ArrayList object, languages, is converted to a string.
🌐
Codecademy
codecademy.com › docs › java › strings › .valueof()
Java | Strings | .valueOf() | Codecademy
July 26, 2025 - The code converts the integer 42 to its string representation "42". This is useful when there is a need to display numeric values in user interfaces or concatenate them with other strings.
🌐
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.
🌐
Java Guides
javaguides.net › 2023 › 09 › java-string-valueof-example.html
Java String valueOf() example
September 25, 2023 - public class ValueOfExample { public static void main(String[] args) { int intVal = 42; char[] charArray = {'H', 'e', 'l', 'l', 'o'}; boolean boolVal = true; // Convert int to String String intString = String.valueOf(intVal); System.out.println("Integer as string: " + intString); // Convert char array to String String charArrayString = String.valueOf(charArray); System.out.println("Char array as string: " + charArrayString); // Convert boolean to String String boolString = String.valueOf(boolVal); System.out.println("Boolean as string: " + boolString); // Convert null object to String Object obj = null; String nullString = String.valueOf(obj); System.out.println("Null object as string: " + nullString); } }
🌐
DEV Community
dev.to › tapuchandramojumder › understanding-valueof-and-tostring-in-javascript-7ph
Understanding valueOf() and toString() in JavaScript - DEV Community
April 21, 2024 - If not defined, JavaScript uses the default method which typically returns "[object Object]". Examples: // Example of valueOf() method const obj1 = { value: 40, valueOf() { return this.value; } };