Best way to “negate” an instanceof

后端 未结 9 1327
后悔当初
后悔当初 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:29

    You could use the Class.isInstance method:

    if(!String.class.isInstance(str)) { /* do Something */ }
    

    ... but it is still negated and pretty ugly.

提交回复
热议问题