558

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
3
  • 6
    You could always create a variable, something like boolean strIsString = str instanceof String;... Commented Jan 30, 2012 at 17:42
  • yeah @Baqueta, is a option. But, what differences could happen in memory use in one syntax or another?
    – caarlos0
    Commented Jan 30, 2012 at 20:04
  • 1
    Your example is bad, because String can not have subclasses, so your example is basically an isNull check. Commented Sep 14, 2018 at 12:05

9 Answers 9

408

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

2
  • 1
    Actually there can be like: if (!(obj instanceof String)) - Works just fine!
    – arun
    Commented May 26, 2022 at 10:51
  • 5
    @arun That is the syntax that we all hate, and the original poster is asking how to avoid.
    – Ian Boyd
    Commented Jul 19, 2022 at 19:27
169

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) { /* ... */ }
6
  • 6
    About double logic, you could use != true instead of == false :D
    – jupi
    Commented Nov 16, 2018 at 14:27
  • 5
    Seeing this helps me to understand that if(!(str instanceof String)) is the only right way, and I need to stop thinking alternatives
    – Vikash
    Commented May 11, 2019 at 7:31
  • I like this solution since I am not required to build up a metal stack while reading it!
    – JaM
    Commented Aug 9, 2019 at 12:59
  • SonarLint will report an issue about this coding form (as a minor Code Smell), which message is: Remove the literal "false" boolean value.
    – Ray Chen
    Commented Dec 4, 2020 at 10:43
  • static code checkers complain about this, but I prefer this, that small ! character is so easy to overlook... And I'd say there is a lot of "us", e.g. StringUtils offers both isBlank() and isNotBlank() compared to !isBlank()...
    – Betlista
    Commented Jan 14, 2022 at 14:12
65

You could use the Class.isInstance method:

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

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

4
  • 5
    is a little bit better, the excess parenthesis makes the code ugly, IMHO.
    – caarlos0
    Commented Jan 30, 2012 at 18:02
  • 1
    Isn't this a lot of slower?
    – maxammann
    Commented Aug 2, 2013 at 20:54
  • 4
    This has different behaviour. The instanceof keyword includes subclasses, the method does not, you need to use Class.isAssignableFrom to replicate the behaviour. Commented Feb 4, 2014 at 17:11
  • 9
    @ChrisCooper This is not true: this method returns true if the specified Object argument is an instance of the represented class (or of any of its subclasses)
    – Natix
    Commented Aug 19, 2014 at 14:51
34

Usually you don't want just an if but an else clause as well.

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

can be written as

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

Or you can write the code so you don't need to know if its a String or not. e.g.

if(!(str instanceof String)) { str = str.toString(); } 

can be written as

str = str.toString();
3
  • I think this is the best answer here(first part) Commented Oct 16, 2020 at 14:57
  • This must be an acceptable answer as IntelliJ also suggested this method.
    – Udhaya
    Commented Jun 7, 2021 at 6:24
  • This might just be a "me" problem, but if not and others are in a similar situation, I wanted to at least add the comment that my first reading of the first part was "most of the time it's good practice to always include an else for your if, even if it's just a no-op", which I'd call bad advice. I now realize I misread that, but wanted to drop this note in case anyone else misreads it as well. Great answer!
    – acat
    Commented Feb 4, 2022 at 5:58
13

If you can use static imports, and your moral code allows them

public class ObjectUtils {
    private final Object obj;
    private ObjectUtils(Object obj) {
        this.obj = obj;
    }

    public static ObjectUtils thisObj(Object obj){
        return new ObjectUtils(obj);
    }

    public boolean isNotA(Class<?> clazz){
        return !clazz.isInstance(obj);
    }
}

And then...

import static notinstanceof.ObjectUtils.*;

public class Main {

    public static void main(String[] args) {
        String a = "";
        if (thisObj(a).isNotA(String.class)) {
            System.out.println("It is not a String");
        }
        if (thisObj(a).isNotA(Integer.class)) {
            System.out.println("It is not an Integer");
        }
    }    
}

This is just a fluent interface exercise, I'd never use that in real life code!
Go for your classic way, it won't confuse anyone else reading your code!

1
  • I don't like static imports.. anyway thanks for try to help :)
    – caarlos0
    Commented Jan 30, 2012 at 19:19
6

If you find it more understandable, you can do something like this with Java 8 :

public static final Predicate<Object> isInstanceOfTheClass = 
    objectToTest -> objectToTest instanceof TheClass;

public static final Predicate<Object> isNotInstanceOfTheClass = 
    isInstanceOfTheClass.negate(); // or objectToTest -> !(objectToTest instanceof TheClass)

if (isNotInstanceOfTheClass.test(myObject)) {
    // do something
}
1
  • 1
    With Java 11, this should work if (Predicate.not(isInstanceOfTheClass).test(myObject)) { .... Not better, imo, but should work!
    – Patrick M
    Commented Dec 4, 2019 at 21:14
3

ok just my two cents, use a is string method:

public static boolean isString(Object thing) {
    return thing instanceof String;
}

public void someMethod(Object thing){
    if (!isString(thing)) {
        return null;
    }
    log.debug("my thing is valid");
}
1
  • Your code doesn't even compile.
    – jmizv
    Commented May 16, 2023 at 9:42
1

I agree that in most cases the if (!(x instanceof Y)) {...} is the best approach, but in some cases creating an isY(x) function so you can if (!isY(x)) {...} is worthwhile.

I'm a typescript novice, and I've bumped into this S/O question a bunch of times over the last few weeks, so for the googlers the typescript way to do this is to create a typeguard like this:

typeGuards.ts

export function isHTMLInputElement (value: any): value is HTMLInputElement {
  return value instanceof HTMLInputElement
}

usage

if (!isHTMLInputElement(x)) throw new RangeError()
// do something with an HTMLInputElement

I guess the only reason why this might be appropriate in typescript and not regular js is that typeguards are a common convention, so if you're writing them for other interfaces, it's reasonable / understandable / natural to write them for classes too.

There's more detail about user defined type guards like this in the docs

0
-1

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
1
  • That syntax is already in the question, and that's the syntax the asker wants to avoid.
    – Sören
    Commented Apr 27 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.