17

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?

5
  • 1
    Instead of using instanceof you can always, of course, cast to the presumed object type and listen for the ClassCastException. Commented Feb 18, 2012 at 2:35
  • 2
    @HotLicks: True. One small difference is that if obj is null, then (obj instanceof ____) will be false, but ((____) obj) will not cause an exception to be raised. Commented Feb 18, 2012 at 2:43
  • Your design is suspect if you have to use 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. Commented Feb 18, 2012 at 2:49
  • @ruakh -- Correct. (And I wasn't advocating the use of exception catching vs instanceof, just pointing out that it would work too.) Commented Feb 18, 2012 at 13:12
  • check this POST [SOLVED] - How to check if a string is a double safely in JAVA hongouru.blogspot.com.uy/2016/02/… Commented Feb 7, 2016 at 16:33

2 Answers 2

39

Yes:

 if(obj instanceof String)
 {
     String str = (String) obj;
     .
     .
     .
 }

By the way, to clarify regarding this:

[…] test something about the newly casted object to see if its in fact the type of object into which it was casted?

You cannot cast something into an invalid type. If obj has type String, then ((Integer)obj) will cause a ClassCastException to be raised at run-time.

Sign up to request clarification or add additional context in comments.

4 Comments

One thing to note, is that instanceof returns false when obj is null
So if I out.println( ) the object and I see "[Ljava.lang.Object;@4296e599" what does this mean? I thought it was a Map or HashMap but I get false for these when testing instance of.
That means that it has type Object[]. You can use System.out.println(java.util.Arrays.toString((Object[]) ____)), where ____ is the variable-name, to print out the elements of the array.
I meet this problem in the onsite interview yesterday. The interviewer asked me to sum (Collection<Object> objects). The objects are ( string, number, List, Set or HashMap). I need to check the obj's type, and sum the values of the objects. Skip the strings of Objects. Thanks!
8

You are looking for the instanceof operator.

The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.

Example: "Hello" instanceof String would return true while new Integer(5) instanceof String would return false.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.