I have an Object in java. Is there a way to check if an object is an instance of a String, HashMap, or HashMap[ ] before actually casting it to those objects?
If not, as it seems counterintuitive that the above would work, is there a way to cast it into each object, and test something about the newly casted object to see if its in fact the type of object into which it was casted?
instanceofyou can always, of course, cast to the presumed object type and listen for theClassCastException.objisnull, then(obj instanceof ____)will befalse, but((____) obj)will not cause an exception to be raised.instanceof. You're checking to see if you have a String, a HashMap, or a HashMap array? Where's the abstraction and information hiding in that? Java's an object-oriented language. I don't know what problem you're solving, but it sounds like you're lost. You should think of a better abstraction than this.instanceof, just pointing out that it would work too.)