I was thinking if there exists a better/nicer way to negate an instanceof
in Java.
Actually, I'm doing something like:
if(!(myObject instanceof SomeClass)) { /* do Something */ }
But I think that a "beautiful" syntax to do this should exist.
Does anyone know if it exists, and how the syntax look like?
EDIT: By beautiful, I might say something like this:
if(myObject !instanceof SomeClass) { /* do Something */ } // compilation fails
No, there is no better way; yours is canonical.
I don't know what you imagine when you say "beautiful", but what about this? I personally think it's worse than the classic form you posted, but somebody might like it...
if (str instanceof String == false) { /* ... */ }
You could use the Class.isInstance
method:
if(!String.class.isInstance(str)) { /* do Something */ }
... but it is still negated and pretty ugly.