What is the difference between
Object foo = \"something\"; String bar = String.valueOf(foo);
and
Object foo = \"something\"
String.valueOf(foo) invokes foo's .toString() method and assigns the result to the the bar. It is null and type safe operation.
String.valueOf(foo)
.toString()
Casting will just assign foo to the bar, if the types are matching. Otherwise, the expression will throw a ClassCastException.
ClassCastException