19

Is it possible to get the opposite of instanceof in java? I have tried code like this:

if( example !instanceof blarg)....

but it won't let me put the ! anywhere without an error, please help.

Lolmister
  • 243
  • 1
  • 3
  • 7

2 Answers2

67

You have to negate the entire thing:

if(!(example instanceof blarg))

You could also write it like so:

if(example instanceof blarg == false)
C_B
  • 2,620
  • 3
  • 23
  • 45
Corbin
  • 33,060
  • 6
  • 68
  • 78
1

As an alternative make use of isInstance method:

   if (!example.class.isInstance(blarg))
   {
     // your code here
   }
Ayaz Alifov
  • 8,334
  • 4
  • 61
  • 56