0

So i have defined the main class to be shown below and i have defined a words class and a sentence class. Note the program should return false when ran. However, i am getting a "Incompatible conditional operand types words and sentence" error when i run it. Isn't this how the instanceof operator is to be used or am i confused? How can my program be modified to run without crashing?

public class main{
public static void main (String[] args){
    words example = new words("heyo");
    sentence ex = new sentence("wats up dog");

    System.out.println(example instanceof sentence);
}
}
  • Why do you think it should return false? – SMA Dec 16 '14 at 08:38
  • 1
    You need to pass instanceof something that could plausibly return true. Otherwise, it is resolved at compile time as an error. – lea Dec 16 '14 at 08:45

5 Answers5

4

If the variable you are running instanceof on can't be of the same type as the class you supply to the operator, the code won't compile.

Since example is of type words, which is not a sub-class or super-class of sentence, instanceof cannot be applied on example and sentence.

Here's what JLS 15.20.2 says about the expression "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.

At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast (§15.16) to the ReferenceType without raising a ClassCastException. Otherwise the result is false.

Community
  • 1
  • 1
Eran
  • 387,369
  • 54
  • 702
  • 768
0

Your class i.e. sentence should be in the same hierarchy as your example is from (i.e. type or subtype of word) for this to compile. Here are the examples of the same and this is hwat it says about instanceof:

The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.
SMA
  • 36,381
  • 8
  • 49
  • 73
0

If words is not a subclass of sentence class, there is no way an example object is instance of sentence.

peterremec
  • 488
  • 1
  • 15
  • 46
0

Instance of method is applicable only for class in same hierachy. For example :-

Class A{
}

Class B extends A {
}

Class C{
}
// in your main 
A a = new B();
// then you can check if a is object of B
System.out.println(a instanceOf B);
//You cannot do 

System.out.println(a instance of C);
// as no way you cannot do this
  A a = new C();
Panther
  • 3,312
  • 9
  • 27
  • 50
0

In the instanceof operator, if the LHS (instance) is not an instance of the RHS (Class) then it returns false and vice-versa. However, in your case, it is trivial for the compiler to resolve that these are incompatible types so it complains.

So something like the following (similar to your example):

String a = "asd";
Integer b = new Integer(12);
System.out.println(""+ (b instanceof String)); // compile time error

will never compile. However if the compiler cannot determine as in the following case then when you run it would return false:

public static void main(String[] args) {

  String a = "asd";
  Integer b = new Integer(12);
  check (b); // prints false

}

public static void check(Object o) {

  System.out.println(o instanceof String);
}
Khanna111
  • 3,627
  • 1
  • 23
  • 25