instanceof - incompatible conditional operand types

java_geek picture java_geek · Mar 31, 2010 · Viewed 46.2k times · Source

The following compiles fine:

  Object o = new Object();
  System.out.println(o instanceof Cloneable);

But this doesn't:

  String s = new String();
  System.out.println(s instanceof Cloneable);

A compiler error is thrown.

What is the problem?

Answer

Some Guy picture Some Guy · Jan 4, 2012

A related issue that I have come across recently (and which led me to this page, before I figured out what was going on) is that the Eclipse environment can report "Incompatible conditional operand types" in an 'instanceof' expression erroneously due to a missing 'import' statement for the type on the right of the 'instanceof'. I spent a while trying to figure out how the types in question could possibly be incompatible before figuring out that a missing import was causing the whole problem. Hopefully this information saves somebody some time.