In Java, the developers often come across the situations where it is required to return the string representation against the passed values. This transformation is done to apply the string operations upon the values conveniently. In addition to that, Java provides the functionality to return a limitation as well upon the found “null” value whilst transforming the value to string. In such case-scenarios, the Java “toString()” and “valueOf()” methods serve the purpose of transformation to string.
What is the Difference Between “toString()” and “valueOf()” in Java?
Both the “toString()” method of the String class and the “valueOf()” method gives a string against the passed value.
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.
On the other hand, the “valueOf()” method returns a “null” string if the passed value is null and can be utilized with primitive types.
Syntax (“toString()” Method)
String toString(int i)
In the given syntax, “i” refers to the integer for which the string representation is to be retrieved.
Syntax (“valueOf()” Method)
String.valueOf(z)
In this syntax, “z” can be of any data type i.e., boolean, float, int, long and is transformed into a string.
Example 1: Applying the “toString()” and “valueOf()” Methods Upon the Object Value
This demonstration applies both the “toString()” and “valueOf()” methods to return the string representation and the value of the defined object value, respectively:
public class ValueoftoString {
public static void main(String[] args) {
Object x = "Java Programming!";
System.out.println("valueOf() Method on Object -> " + String.valueOf(x));
System.out.println("toString() Method on Object -> " + x.toString());
}}
In the above code lines:
- Initialize the provided string value in an “Object” type.
- In the next step, apply the “valueOf()” method to retrieve the value of the defined value as a string.
- Lastly, associate the “toString()” method with the string to retrieve the string.
Output
Example 2: Applying the “toString()” and “valueOf()” Methods Upon the “int” and “char” Values
In this specific example, the methods differentiation can be analyzed upon the “int” and “char” data type values:
public class ValueoftoString {
public static void main(String[] args) {
int x = 10;
char y = 'a';
System.out.println("valueOf() Method on Integer -> " + String.valueOf(x));
System.out.println("toString() Method on Integer -> " + x.toString());
System.out.println("valueOf() Method on Character -> " + String.valueOf(y));
System.out.println("toString() Method on Character -> " + y.toString());
}}
According to this block of code, define an “int” and a “char” type value. Now, apply the “valueOf()” and “toString()” methods firstly upon the “int” value and then on the “char” value.
Output
From this output, it can be verified that the “toString()” method returned an exception upon being applied on the primitive data types “int” and “char”.
Example 3: Applying the “toString()” and “valueOf()” Methods Upon the “null” Value
This example implements both the methods upon the defined “null” value:
public class ValueoftoString {
public static void main(String[] args) {
Object x = null;
System.out.println("valueOf() Method -> " + String.valueOf(x));
System.out.println("toString() Method -> " + x.toString());
}}
In this snippet of code, firstly, define a “null” value. After that, apply both the methods upon the “null” value to analyze the outcome.
Output
In the outcome, as discussed, the “valueOf()” method returns a “null” string and the “toString()” method returns the “NullPointerException”.
Conclusion
Both the “toString()” method and the “valueOf()” method returns a string representation against the passed value. The limitation with the former method is that it cannot be applied to the “null” values and throws an exception instead whereas the latter method can be implemented to the null values.