Difference between casting to String and String.valueOf

JavaCasting

Java Problem Overview


What is the difference between

Object foo = "something";
String bar = String.valueOf(foo);

and

Object foo = "something";
String bar = (String) foo;

Java Solutions


Solution 1 - Java

Casting to string only works when the object actually is a string:

Object reallyAString = "foo";
String str = (String) reallyAString; // works.

It won't work when the object is something else:

Object notAString = new Integer(42);
String str = (String) notAString; // will throw a ClassCastException

String.valueOf() however will try to convert whatever you pass into it to a String. It handles both primitives (42) and objects (new Integer(42), using that object's toString()):

String str;
str = String.valueOf(new Integer(42)); // str will hold "42"
str = String.valueOf("foo"); // str will hold "foo"
Object nullValue = null;
str = String.valueOf(nullValue); // str will hold "null"

Note especially the last example: passing null to String.valueOf() will return the string "null".

Solution 2 - Java

String.valueOf(foo) invokes foo's .toString() method and assigns the result to the the bar. It is null and type safe operation.

Casting will just assign foo to the bar, if the types are matching. Otherwise, the expression will throw a ClassCastException.

Solution 3 - Java

Casting means that the object needs to be of type String, while String.valueOf() can take other types as well.

Solution 4 - Java

Both generates same output in case of String.

Casting fails in case of provided object is Not a string.

Solution 5 - Java

String.valueOf method is used to get the String represenation of it's parameter object.

(String) value casts object value to string.

You can use the String.valueOf method to get the String representation of an object without worrying about null references. If you try to cast String on a null reference you would get a NullPointerException.

Solution 6 - Java

> final Object obj = null; > final String strValOfObj = String.valueOf(obj); > final String strCastOfObj = (String) obj; > if (strValOfObj == null) System.out.println("strValOfObj is null"); > if (strCastOfObj == null) System.out.println("strCastOfObj is null");

Output: strCastOfObj is null

Solution 7 - Java

The first one i.e, String.valueOf returns a string only if the object is a representable type which is a value type or a String.. Else it throws the exception.

In the latter one, you are directly casting which can fail if the object isn't a string.

Online example.

http://ideone.com/p7AGh5

Solution 8 - Java

in String.valueOf(); string as work typecasting all the argument passed in valueof() method convert in String and just like integer.string() convert integer into string only

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDropoutView Question on Stackoverflow
Solution 1 - JavaJoachim SauerView Answer on Stackoverflow
Solution 2 - JavadarijanView Answer on Stackoverflow
Solution 3 - JavatstormsView Answer on Stackoverflow
Solution 4 - JavaSuresh AttaView Answer on Stackoverflow
Solution 5 - JavaSyamantak BasuView Answer on Stackoverflow
Solution 6 - JavaZakiView Answer on Stackoverflow
Solution 7 - JavaSri Harsha ChilakapatiView Answer on Stackoverflow
Solution 8 - JavaanuragsinghView Answer on Stackoverflow