Do this instead
String s = String.valueOf(o);
You cannot cast an Object to String if it is not a String object. You'll need to invoke toString
method on the object instead - unless the object is null
. String.valueOf()
takes care of that
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}