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 theinstanceof
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 classString
does not implementCloneable
- 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
How to Get Information About the Local Variables Using Java Reflection
What Are the Pros and Cons of the Assorted Java Web Frameworks
Java: Why Is the Date Constructor Deprecated, and What Do I Use Instead
Converting JSON to Xls/CSV in Java
Differencebetween @Inject and @Autowired in Spring Framework? Which One to Use Under What Condition
Can an Array Be Used as a Hashmap Key
Varargs and the '...' Argument
How to Redirect to Login Page When Session Is Expired in Java Web Application
Why Business Logic Should Be Moved Out of Jsp
How to Convert a 3D Point into 2D Perspective Projection
What Is the Default Access Modifier in Java
Testing Private Method Using Mockito
How to Convert a Date String to a Date or Calendar Object
Why Use a Reentrantlock If One Can Use Synchronized(This)
Why Is T Bounded by Object in the Collections.Max() Signature