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
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ java โ€บ strings โ€บ .valueof()
Java | Strings | .valueOf() | Codecademy
July 26, 2025 - This example demonstrates converting multiple data types to strings for creating a formatted user report, which is common in business applications. This example illustrates using .valueOf() in a practical scenario where dynamic SQL queries are constructed using different data types:
๐ŸŒ
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. valueOf(String s, int radix) โˆ’ This returns an Integer object holding the integer value of the specified string representation, ...
๐ŸŒ
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 Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... char[] myArray = {'a', 'b', 'c'}; System.out.println(String.valueOf(myArray)); ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-string-valueof
Java String valueOf() Method - GeeksforGeeks
December 2, 2024 - ... // Java program to demonstrate ... method public class ValueOf { public static void main(String[] args) { int n = 21; // Declare an integer String s = String.valueOf(n); // Convert integer to string System.out.println("String representation: " + s); } }...
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ library โ€บ string โ€บ valueof
Java String valueOf()
Become a certified Java programmer. Try Programiz PRO! ... The valueOf() method returns the string representation of the argument passed. class Main { public static void main(String[] args) { double interest = 923.234d;
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ string valueof() in java
String valueOf() in Java - Scaler Topics
April 8, 2024 - 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. ... Thus, any object can be passed to valueOf(), and we would not get any error, as internally, the ...
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()
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2017 โ€บ 10 โ€บ java-string-valueof-method
Java String valueOf() method
In this example we are concatenating the double nines to the end of the given value. The given value is an integer, in order to append 99 at the end of the integer we must need to convert the given integer to the string first. We are using valueOf() method to convert the number to the equivalent ...
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ javas-string-valueof-method-explained-b3fba964d3ec
Java String.valueOf() Method Explained | Medium
July 24, 2024 - ... for (int i = 0; i < 1000000; i++) { String.valueOf(i); // Creates and discards a large number of string objects } In this example, a large number of temporary string objects are created and discarded, which can put pressure on the garbage ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ integer-valueof-method-in-java
Integer valueOf() Method in Java - GeeksforGeeks
July 2, 2018 - public class Geeks { public static void main(String[] args) { Integer value = Integer.valueOf(-9185); System.out.println("Output Value = " + value); } } ... Negative integers are also supported.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ home โ€บ java โ€บ java string valueof method
Java String valueOf Method
September 1, 2008 - This method returns the string representation of the passed argument. valueOf(boolean b) โˆ’ Returns the string representation of the boolean argument. valueOf(char c) โˆ’ Returns the string representation of the char argument. valueOf(char[] ...
๐ŸŒ
W3Resource
w3resource.com โ€บ java-tutorial โ€บ string โ€บ string_valueof.php
Java String: valueOf Method - w3resource
The following example shows the ... method. public class StringValueOfInteger { public static void main(String[] args) { int val = 1; Integer i = 50; System.out.println(String.valueOf(val)); System.out.println(String.valueOf(i)); System.out.println(String.valueOf(-3)); } }...
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2024 โ€บ 06 โ€บ valueof-method-in-java
ValueOf() Method in Java
public class ValueOfExample { public static void main(String[] args) { // From int to Integer Integer intObj1 = Integer.valueOf(101); // From String to Integer Integer intObj2 = Integer.valueOf("200"); System.out.println(intObj1); // Output: 101 System.out.println(intObj2); // Output: 200 } }
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ javas-enum-valueof-method-explained-9e686f7c806a
Javaโ€™s Enum.valueOf() Method Explained | Medium
October 7, 2024 - For example, a simple form that allows users to choose a priority level (e.g., LOW, MEDIUM, HIGH) can use Enum.valueOf() to map the input string to an enum: import java.util.Scanner; public class UserInputExample { public enum Priority { LOW, ...
๐ŸŒ
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 * convert boolean to string * convert character t...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-string-valueof
Data Conversion Using valueOf() method in Java
December 2, 2024 - ... // Java program to demonstrate ... method public class ValueOf { public static void main(String[] args) { int n = 21; // Declare an integer String s = String.valueOf(n); // Convert integer to string System.out.println("String representation: " + s); } }...
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ api โ€บ java.lang.integer.valueof
Integer.ValueOf Method (Java.Lang) | Microsoft Learn
Returns an Integer instance representing the specified int value. [Android.Runtime.Register("valueOf", "(I)Ljava/lang/Integer;", "")] public static Java.Lang.Integer ValueOf(int i); [<Android.Runtime.Register("valueOf", "(I)Ljava/lang/Integer;", ...
๐ŸŒ
TechVidvan
techvidvan.com โ€บ tutorials โ€บ java-string-valueof-method
Java String valueOf() Method - TechVidvan
November 11, 2024 - This function helps convert a string object to a string. ... class Main { public static void main(String[] args) { double interest = 923.443d; System.out.println(String.valueOf(interest)); } }
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ java-how-to-utilize-the-integer-valueof-method-in-java-414178
How to utilize the Integer.valueOf() method in Java | LabEx
Integer i1 = Integer.valueOf("42"); Integer i2 = Integer.valueOf("42"); System.out.println(i1 == i2); // Output: true ยท In the above example, since both i1 and i2 are assigned the value 42, which is within the cached range, the method returns ...