Tôi muốn biết về hành vi sau của instanceoftoán tử trong Java.
interface C {}
class B {}
public class A {
public static void main(String args[]) {
B obj = new B();
System.out.println(obj instanceof A); //Gives compiler error
System.out.println(obj instanceof C); //Gives false as output
}
}
Tại sao nó như vậy? Không có mối quan hệ nào giữa interface Cvà class B, nhưng nó cho sai trong khi trong trường hợp obj instanceof Anó cho lỗi trình biên dịch?
Object obj = new B(), nó sẽ biên dịch.