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
🌐
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.
🌐
YouTube
youtube.com › watch
Java String valueOf() method | string.valueof in java | valueOf() method in java - YouTube
The java string valueOf() method converts different types of values into string. By the help of string valueOf() method, you can convert int to string, long ...
Published   May 21, 2022
Discussions

java - String.valueOf() vs. Object.toString() - Stack Overflow
It turns out that String.valueOf(null) (note the difference!) does give an NPE ... despite the javadoc. The real explanation1 is obscure: There are a number of overloads of String.valueOf, but there are two that are relevant here: String.valueOf(Object) and String.valueOf(char[]). In the expression ... More on stackoverflow.com
🌐 stackoverflow.com
Cleanest way to check for null on a String?
If all you're doing is returning an Optional, use the features of the Optional: return Optional.ofNullable(someObject.get("someKey")); If you need to do some additional processing on the string value, call .map() on the Optional. More on reddit.com
🌐 r/java
11
3
May 8, 2024
parseInt() vs valueOf() when to use each?

Integer.parseInt() returns a primitive. valueOf() returns an Integer object. Call the one that matches the return type you need and avoid autoboxing/unboxing.

More on reddit.com
🌐 r/learnjava
6
6
August 28, 2022
Converting a Number to a String
The docs will answer that question quite clearly https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#toString(int) https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#valueOf(int) under the String.valueOf(int) description: The representation is exactly the one returned by the Integer.toString method of one argument. to make it even more clear if we look at the source code for String.valueOf(int) https://hg.openjdk.java.net/jdk7u/jdk7u6/jdk/file/8c2c5d63a17e/src/share/classes/java/lang/String.java we find that it literally just calls Integer.toString(int) public static String valueOf(int i) { return Integer.toString(i); } More on reddit.com
🌐 r/javahelp
3
6
August 14, 2022
🌐
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 - * @see java.lang.Integer#toString(int, int) */ public static String valueOf(int i) { return Integer.toString(i); } To understand it better, we’ll see a few examples where we pass signed/unsigned integers as parameters to it to understand integer to string conversion happens: @Test public void whenValidIntIsPassed_thenShouldConvertToValidString() { assertEquals("11", String.valueOf(11)); assertEquals("11", String.valueOf(+11)); assertEquals("-11", String.valueOf(-11)); } To summarize, there are no actual differences between these two methods, but we should understand the following points to avoid confusion.
🌐
Programiz
programiz.com › java-programming › library › string › valueof
Java String valueOf()
Java ArrayList toArray() The valueOf() method returns the string representation of the argument passed. class Main { public static void main(String[] args) { double interest = 923.234d; // convert double to string System.out.println(String.valueOf(interest)); } } // Output: 923.234 ·
🌐
W3Schools
w3schools.com › java › ref_string_valueof.asp
Java String valueOf() Method
assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof int interface long module native new package private protected public return requires short static super switch synchronized this throw throws transient try var void volatile while Java String Methods
Find elsewhere
Top answer
1 of 11
267

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
26

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 | Medium
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.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.string.valueof
String.ValueOf Method (Java.Lang) | Microsoft Learn
Microsoft makes no warranties, express or implied, with respect to the information provided here. Returns the string representation of the long argument. [Android.Runtime.Register("valueOf", "(J)Ljava/lang/String;", "")] public static string ...
🌐
Baeldung
baeldung.com › home › java › java string › java string.valueof()
Java.String.valueOf() | Baeldung
April 10, 2025 - • Java String.String() • Java String.codePointCount() • Java String.codePointAt() • Java String.concat() • Java String.contains() • Java String.copyValueOf() • Java String.endsWith() • Java String.format() • Java String.getBytes() • Java String.indexOf() • Java String.intern() • Java String.isEmpty() • Java String.lastIndexOf() • Java String.regionMatches() • Java String.replace() • Java String.replaceAll() • Java String.split() • Java String.startsWith() • Java String.subSequence() • Java String.substring() • Java String.toLowerCase() • Java String.toUpperCase() • Java String.trim() ... The method valueOf() has several overloads that accept one parameter of different types and convert them to a String.
🌐
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 = ...
🌐
Quora
quora.com › What-is-the-use-of-the-String-ValueOf-function-in-Java
What is the use of the String.ValueOf function in Java? - Quora
Answer (1 of 2): The valueOf() method is used to converts different types of values into a string. You can convert different type of data value into String data value. You can convert different types: * int to string * convert long to string ...
🌐
Tutorialspoint
tutorialspoint.com › home › java › java number valueof method
Java Number ValueOf Method
September 1, 2008 - valueOf(String s) − This returns an Integer object holding the value of the specified string representation.
🌐
Java String
javastring.net › home › java string valueof() method examples
Java String valueOf() Method Examples
August 6, 2019 - jshell> String doubleStr = String.valueOf(12.34d); doubleStr ==> "12.34" jshell> String doubleStr = String.valueOf(12.34D); doubleStr ==> "12.34" jshell> String doubleStr = String.valueOf(Double.MAX_VALUE); doubleStr ==> "1.7976931348623157E308" jshell> String doubleStr = String.valueOf(Double.MIN_VALUE); doubleStr ==> "4.9E-324" jshell> String doubleStr = String.valueOf(Double.POSITIVE_INFINITY); doubleStr ==> "Infinity" jshell> String doubleStr = String.valueOf(Double.NEGATIVE_INFINITY); doubleStr ==> "-Infinity" jshell> String objStr = String.valueOf(new Object()); objStr ==> "java.lang.Object@1698c449" jshell> String objStr = String.valueOf(new Exception("Exception Message")); objStr ==> "java.lang.Exception: Exception Message"
🌐
TechVidvan
techvidvan.com › tutorials › java-string-valueof-method
Java String valueOf() Method - TechVidvan
November 11, 2024 - Using the string valueOf() method, we can convert int to string, long to string, boolean to string, character to string, float to string, double to string, object to string, and char array to string. The Java valueOf method can transform different types of values into strings.
🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › java string valueof() method
Java String valueOf() Method Explained
September 1, 2008 - The java.lang.String.valueOf(Object obj) method returns the string representation of the Object argument.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › valueOf
String.prototype.valueOf() - JavaScript | MDN
const stringObj = new String("foo"); console.log(stringObj); // Expected output: String { "foo" } console.log(stringObj.valueOf()); // Expected output: "foo"
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › String.html
String (Java Platform SE 7 )
Java™ Platform Standard Ed. 7 ... The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
October 20, 2025 - Trailing empty strings are therefore not included in the resulting array. The string "boo:and:foo", for example, yields the following results with these expressions: ... the array of strings computed by splitting this string around matches of the given regular expression ... Returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter. ... String message = String.join("-", "Java", "is", "cool"); // message returned is: "Java-is-cool" Note that if an element is null, then "null" is added.