100

I want to check if an object o is an instance of the class C or of a subclass of C.

For instance, if x is of class Point I want x.instanceOf(Point.class) to be true and also x.instanceOf(Object.class) to be true.

I want it to work also for boxed primitive types. For instance, if x is an Integer then x.instanceOf(Integer.class) should be true.

Is there such a thing? If not, how can I implement such a method?

Turing85
  • 18,217
  • 7
  • 33
  • 58
snakile
  • 52,936
  • 62
  • 169
  • 241

8 Answers8

194

Class.isInstance does what you want.

if (Point.class.isInstance(someObj)){
    ...
}

Of course, you shouldn't use it if you could use instanceof instead, but for reflection scenarios it often comes in handy.

gustafc
  • 28,465
  • 7
  • 73
  • 99
  • 6
    @Ahamed it's probably slower, but what's worse is it's harder to understand! If there is a very simple way to do something - like `if (a instanceof Point)` - but you do it in a more convoluted way, whoever reads the code is bound to think "oh, there has to be a reason for this being done the hard way", and then jump to the docs to find out how `Class.isInstance` differs from `instanceof`. You waste your colleagues' time. – gustafc Aug 25 '14 at 10:12
21

I want to check if an object o is an instance of the class c or of a subclass of c. For instance, if p is of class Point I want x.instanceOf(Point.class)

Um... What? What are o, p and x?

I want it to work also for primitive types. For instance, if x is an integer then x.instanceOf(Integer.class) and also x.instanceOf(Object.class) should be true.

No. It shouldn't even compile. Primitives are not objects, and you cannot call methods on them.

Anyway, there are three things, one of which can definitely achieve what you want (they differ somewhat in where exactly the apply:

  • The instanceof operator if you know the class at compile time.
  • Class.isInstance() if you want to check an object's class against a class not known at compile time.
  • Class.isAssignableFrom() if you want to check the assignability given two class objects.
Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
10
x instanceof Integer
x instanceof Object

you just have to use the right syntax

for primitve types, you have to do it completely different. Since you cannot create methods for them , you need a class that keeps the method. So instead of "x.instanceOf(Integer.Class)", you have to call "MyClassComparer.instanceOf(x, Integer.Class)" or something like that. This could easily be implemented by overloading methods, but I fail to see a case when that functionality would be desireable at all.

Erich Kitzmueller
  • 36,381
  • 5
  • 80
  • 102
4

In fact in Java there's a boolean operator called instanceof which can be used to determine 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.

if(obj instanceof SomeClass) {
    // Do something
}

The Java Tutorial has a good example of this operator

victor hugo
  • 35,514
  • 12
  • 68
  • 79
  • 1
    you cannot use instanceof with primitive types – dfa Jun 04 '09 at 09:02
  • 4
    He confused primitive types with objects. Please read... "For instance, if x is an integer then x.instanceOf(Integer.class) and also x.instanceOf(Object.class) should be true." Shame on you down-voting all the answers – victor hugo Jun 04 '09 at 09:05
  • Seconding (sp) the suggestion of using the Java Tutorial. It is a good reference for learning the language itself. – luis.espinal Apr 02 '10 at 15:58
3

The right-hand side of Class.isAssignableFrom() is the subclass

So, for Objects obj and superclass Class<?> superCls

superCls.isAssignableFrom( obj.getClass() )

and

obj instanceof superCls

are equivalent.

Sam Ginrich
  • 661
  • 6
  • 7
0

You can do:

if (foo instanceof classNameYouWantToCheck) 
fmsf
  • 36,317
  • 49
  • 147
  • 195
0

"I want it to work also for primitive types. For instance, if x is an integer then x.instanceOf(Integer.class) and also x.instanceOf(Object.class) should be true."

Why? Primitive types and reference types are completely separate. A primitive int is not a subtype of Integer nor Object. The type of a primitive value is always known statically at compile time, so there is no point in testing its type. Sure, you can box a primitive value and then test its type:

(Integer)5 instanceof Integer

But what would be the point?

newacct
  • 119,665
  • 29
  • 163
  • 224
-1

I think you're confused about instanceof for raw objects and generic ones

obj instanceof Class
obj instanceof Class<?> // this is the one you want
Paul Bors
  • 105
  • 6