In Java, is there any difference between String.valueOf(Object) and Object.toString()?
Is there a specific code convention for these?
11 Answers
According to the Java documentation, String.valueOf() returns:
if the argument is
null, then a string equal to"null"; otherwise, the value ofobj.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
}
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()
In Java, is there any difference between String.valueOf(Object) and Object.toString()?
Yes. (And more so if you consider overloading!)
As the javadoc explains, String.valueOf((Object) null) will be treated as a special case by the valueOf method and the value "null" is returned. By contrast, null.toString() will just give you an NPE.
Overloading
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)andString.valueOf(char[]).In the expression
String.valueOf(null), both of those overloads are applicable, sincenullis assignment compatible with any reference type.When there are two or more applicable overloads, the JLS says that the overload for the most specific argument type is chosen.
Since
char[]is a subtype ofObject, it is more specific.Therefore the
String.valueOf(char[])overload is called.String.valueOf(char[])throws an NPE if its argument is a null array. UnlikeString.valueOf(Object), it doesn't treatnullas a special case.
Another example illustrates the difference in the valueOf(char[]) overload even more clearly:
char[] abc = new char[]('a', 'b', 'c');
System.out.println(String.valueOf(abc)); // prints "abc"
System.out.println(abc.toString()); // prints "[C@...."
Is there a specific code convention for these?
No.
Use which ever is most appropriate to the requirements of the context in which you are using it. (Do you need the formatting to work for null?)
Note: that isn't a code convention. It is just common sense programming. It is more important that your code is correct than it is to follow some stylistic convention or "best practice" dogma2.
1 - You can confirm this by using javap -c to examine the code of a method that has a String.valueOf(null) call. Observe the overload that is used for the call.
2 - Please read "No Best Practices", and pass this reference on to the next person who tells you that it is "best practice" to do something in the programming or IT domains.