OBJECT.toString() calls the object's toString() method. It cannot be used for primitives, and it will give NullPointerException if OBJECT is null.
String.valueOf(OBJECT) will call the object's toString() method if OBJECT is a non-null reference. If OBJECT is a null reference, it will return the string "null". If OBJECT is a primitive, it will return a String representation of that primitive, probably by calling toString on an appropriate wrapper object, or a static String.valueOf method on the appropriate wrapper class.
Both toString() and String.valueOf are just methods. There's nothing special about them. You can look at their docs and/or source code to see what they do.
(String)OBJECT will attempt to cast the reference OBJECT to a String. I doesn't change the object in any way. If OBJECT is not a reference to one of String's supertypes--Object, Comparable, etc.Then it will be a compile-time error. If OBJECT is a reference to one of String's supertypes, but points to an object at runtime that is not a String, it will be a ClassCastException.
No comments:
Post a Comment