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.
To check if an object is not an instance of a class, use the logical NOT (!) operator to negate the use of the instanceof operator - !( obj instanceof Class) .
instanceof is a binary operator we use to test if an object is of a given type. The result of the operation is either true or false. It's also known as a type comparison operator because it compares the instance with the type. Before casting an unknown object, the instanceof check should always be used.
A pattern is a combination of (1) a predicate, or test, that can be applied to a target, and (2) a set of local variables, known as pattern variables, that are extracted from the target only if the predicate successfully applies to it.
You have to negate the entire thing:
if(!(example instanceof blarg))
You could also write it like so:
if(example instanceof blarg == false)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With