What is the 'instanceof' operator used for in Java?

前端 未结 17 881
梦毁少年i
梦毁少年i 2020-11-22 03:03

What is the instanceof operator used for? I\'ve seen stuff like

if (source instanceof Button) {
    //...
} else {
    //...
}

17条回答
  •  醉话见心
    2020-11-22 03:45

    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.

    http://download.oracle.com/javase/tutorial/java/nutsandbolts/op2.html

提交回复
热议问题