Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (3.9k points)

I was thinking if there exists a better/nicer way to negate an instanceof in Java. Actually, I'm doing something like:

if(!(str instanceof String)) { /* 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?

 By beautiful, I might say something like this:

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

1 Answer

0 votes
by (46k points)
No, there's no better method; yours is official.

Related questions

0 votes
1 answer
asked Sep 30, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
asked Jul 18, 2019 in Java by Shubham (3.9k points)

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...