54

Possible Duplicate:
Java isInstance vs instanceOf operator

When to use Class.isInstance() and when to use instanceof operator?

Java is providing two option for checking assignment compatibility. Which to use when?

Community
  • 1
  • 1
Jyotirup
  • 2,882
  • 9
  • 30
  • 38

2 Answers2

80

For instanceof you need to know the exact class at compile time.

if (foo instanceof ThisClassIKnowRightNow)
   ...

For isInstance the class is decided at run time. (late binding) e.g.

if (someObject.getClass().isInstance(foo))
   ...
user949300
  • 15,364
  • 7
  • 35
  • 66
  • 17
    +1: To the point. Also with Java 8 streams, `.filter(ThisClassIKnowRightNow.class::isInstance)` is slightly more concise than `.filter(o -> o instanceof ThisClassIKnowRightNow)`. – antak Nov 29 '16 at 05:11
  • Suppose in a multi-catch example, I have an example like `try{}catch(A | B | C ex){//here I want to know what type (A/B/C) of exception occurs at runtime. How would we use your approach here?}` – Ram Jul 04 '18 at 18:59
65

I think the official documentation gives you the answer to this one (albeit in a fairly nonspecific way):

This method is the dynamic equivalent of the Java language instanceof operator.

I take that to mean that isInstance() is primarily intended for use in code dealing with type reflection at runtime. In particular, I would say that it exists to handle cases where you might not know in advance the type(s) of class(es) that you want to check for membership of in advance (rare though those cases probably are).

For instance, you can use it to write a method that checks to see if two arbitrarily typed objects are assignment-compatible, like:

public boolean areObjectsAssignable(Object left, Object right) {
    return left.getClass().isInstance(right);
} 

In general, I'd say that using instanceof should be preferred whenever you know the kind of class you want to check against in advance. In those very rare cases where you do not, use isInstance() instead.

aroth
  • 54,026
  • 20
  • 135
  • 176
  • 10
    Another typical use-case for the `isInstance` method is when dealing with generics. There are a lot of situations where you have the `Class` object, and the only way to check whether an object belongs to that class is the `isInstance` method – Robin Jan 01 '12 at 09:45
  • 1
    When not to use `isInstance` may also be helpful. e.g. I was trying to use `if (o.getClass().isInstance(Foo.class)) {...}` which didn't work. The subtly different `if (o.getClass().isInstance(new Foo)) {...}` was what was needed. Of course `if (o instaneOf Foo){...}` also worked, which is probably the correct solution when you are know the type(s). – MikeT Apr 22 '18 at 04:54
  • 1
    @MikeT try: `MyObject.class.isInstance(Foo.class);` and see related post [here](https://stackoverflow.com/a/4140145/3157899) – Naxos84 Mar 18 '19 at 06:37
  • To see if two arbitrarily typed objects are assignment-compatible, there is [isAssignableFrom()](https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#isAssignableFrom-java.lang.Class-). But I don't know if this was the case back in 2012. – glglgl Jan 29 '20 at 19:19