Instanceof - Incompatible Conditional Operand Types

instanceof - incompatible conditional operand types

A more blatant incarnation of your problem is the following:

if ("foo" instanceof Number)
// "Incompatible conditional operand types String and Number"

This is specified in JLS 15.20.2 Type comparison operator instanceof:

RelationalExpression:
RelationalExpression instanceof ReferenceType

If a cast of the RelationalExpression to the ReferenceType would be rejected as a compile-time error, then the instanceof relational expression likewise produces a compile-time error. In such a situation, the result of the instanceof expression could never be true.

That is, since this cast expression generates a compile time error:

(Number) "foo"

so must this expression:

("foo" instanceof Number)

Your case is a bit more subtle, but the principle is the same:

  • String is a final class
  • String does not implement Cloneable
  • Therefore you can't do (Cloneable) aString
  • Therefore also you can't do aString instanceof Cloneable

What causes incompatible conditional operand when using instanceof in Java?

instanceof used to check that given object is of type Class(SomeClass - right side parameter). You cannot use Class to check that instanceof another class. To check class equality or assainability you can use SomeClass.isAssignableFrom(clas)

Incompatible conditional operand types double and double Java(16777232) when using instanceof

The instanceof operator can only be applied to reference types (or null). double and int are primitive types, not reference types.

Java Language Specification, 15.20.02

In your case, for example, an int is an instance of an int, and cannot be otherwise. So there's no need to be able to ask that particular question.

Error on instanceOf when checking a subClass

The instanceof operator tests whether a reference (the left operand) refers to an object which is an instance of the class named on the right operand.

Here, persona will be a reference to an instance of Class, and an instance of Class can never be an instance of Persona. You're testing for an impossible condition, and the compiler is helpfully telling you about that mistake.

I suspect you want Class.isAssignableFrom:

if (Uomo.class.isAssignableFrom(persona))

That would find subclasses of Uomo as well. If you want to test whether it's exactly the Uomo class, you can just use:

if (persona == Uomo.class)

Incompatible conditional operand types void and String

You didn't post the full code for "setMotiRefuse()" method. but i assume it is a regular setter method that returns void.

The issue is in this line of code

result.put("motifPharmacie",computeDto.getPharmacie().getMotiRefuse()!= null ? computeDto.getPharmacie().setMotiRefuse("traitement en chevauchement non pris en charge "): "");

you can't do this in one statement. please split them into two :

computeDto.getPharmacie().setMotiRefuse("traitement en chevauchement non pris en charge ");
result.put("motifPharmacie",computeDto.getPharmacie().getMotiRefuse()!= null ? computeDto.getPharmacie().getMotiRefuse(): "");

The reason is: set method returns void
so the operand "?" sees that you returned void or string which are not compatible. so use get method in the ternary operator "?" instead of set.

why is there a compiler error in this instanceof test

instanceof is used to identify objects polymorphically referenced by a reference variable of another class, so if the reference variable o is not able to reference to an object of class Two it won't compile.

So this:

One o = new One();
if(o instanceof Two) {
System.out.println("ok");
}

as a matter of example would be an error similar to this:

int i = 1;
if (i == "s") {
System.out.println("ok");
}

It can't compile at all!

EDIT

An example of polymorphism where it would make your code compile (as per other's answers):

Object o = new One();

because o can reference to Two, so instanceof will tell you if it does or not.



Related Topics



Leave a reply



Submit