caarlos0 caarlos0 - 1 year ago 104
Java Question

Best way to "negate" an instanceof

I was thinking if exist a better/nicer way to negate a instanceof in Java.
Actually, I do something like:

if(!(str instanceof String)) { /* do Something */ }

But, I think that should exist a "beautiful" syntax to do this.

Someone know if it exists and how the syntax look like?

By beautiful, I might say something like this:

if(str !instanceof String) { /* do Something */ } // compile failure

Answer Source

No, there is no better way; yours is canonical.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download