How can I know if Object is String type object?
Guard your cast with instanceof
String myString;
if (object instanceof String) {
myString = (String) object;
}
Use the instanceof
syntax.
Like so:
Object foo = "";
if( foo instanceof String ) {
// do something String related to foo
}
object instanceof Type
is true
if the object is a Type
or a subclass of Type
object.getClass().equals(Type.class)
is true
only if the object is a Type