How to cast to a specified class type? [duplicate]-Collection of common programming errors
The cast as you write it does nothing.
The cast serves for assigning an object of a superclass to a subclass reference
Object obj = a.toString();
String str = (String) obj; // The compiler can't check that at runtime obj will be
// a String, so the programmer "forces" the compiler to
// allow the assignation
needs a cast, but in
Object obj2 = (String) obj;
the cast does nothing (other than throwing a ClassCastException
if obj
is not a String).