Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

What is the difference between

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

and

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

Thanks!

share|improve this question
Totally same. But it'll be different if Object foo = new Date(); – johnchen902 May 29 at 13:32
10  
Uh, they are not the same ... at all. – Perception May 29 at 13:32

6 Answers

up vote 38 down vote accepted

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"
str = String.valueOf(null); // str will hold "null"

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

share|improve this answer
Lol..you white wash.. +1. – Baadshah May 29 at 13:36
@Joachim Is there any performance difference? – Adam Stelmaszczyk May 29 at 13:37
8  
@AdamStelmaszczyk: I doubt there's any that's relevant. Maybe casting is slightly faster, but the other differences (null handling, ability to handle other types) are just significantly more important than any minor performance difference that there might be. – Joachim Sauer May 29 at 13:38
1  
+1 perfect answer.. :) – Subhrajyoti Majumder May 29 at 13:43
1  
Great answer, thanks! – Dropout May 29 at 13:54
show 2 more comments

String.valueOf(foo) invokes foo's .toString() method, creates a String out of it and assigns it to the the bar.

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

share|improve this answer
1  
+1 For explaining that String.valueOf(Object) invokes Object.toString() – Ryan Amos May 29 at 15:40

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

share|improve this answer

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.

share|improve this answer

Both generates same output in case of String.

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

share|improve this answer
2  
@Downvoter Thanks for the interest towards my post.And the reasonf for downvote?? – Baadshah May 29 at 13:38

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.