Check whether an object is an instance of a specific class:
public class Main {
public static void main(String[] args) {
Main myObj = new Main();
System.out.println(myObj instanceof Main); // returns true
}
}
The instanceof
keyword checks whether an object is an instance of a specific class or an interface.
The instanceof keyword compares the instance with type. The return value is either true
or false
.
Read more about objects in our Java Classes/Objects Tutorial.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!