Best way to “negate” an instanceof

后端 未结 9 1323
后悔当初
后悔当初 2021-01-30 02:01

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))         


        
9条回答
  •  悲哀的现实
    2021-01-30 02:36

    You can achieve by doing below way.. just add a condition by adding bracket if(!(condition with instanceOf)) with the whole condition by adding ! operator at the start just the way mentioned in below code snippets.

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

    instead of

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

提交回复
热议问题