In fact, many times the effect is similar
However, sometimes using. toString () will error, and using string.valueof () will not error
That means there's a difference between the two, in the nuances.
What is the difference between. ToString () and string.valueof ()?
The . toString () description is in the documentation for the JDK:
string.valueof (Object o) If the argument is null and then a String equal to "null"; Otherwise, the value of obj.tostring () is returned.
So, in this particular case, there's going to be an anomaly.
And look at string.valueof ()
1 |
public static String valueOf(Object obj) { |
2 |
return (obj == null) ? "null" : obj.toString(); |
As you can see, the string.valueof () is also called the ToString () method of the object. So if you are using the null parameter, calling the ToString method will report an exception! The valueof () method returns null if it is null, not the string after the ToString () is returned.
We recommend that you choose string.valueof ()
Transfer from http://www.ztyhome.com/android-tostring-string-valueof-diff/