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

前端 未结 17 899
梦毁少年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:54

    class Test48{
    public static void main (String args[]){
    Object Obj=new Hello();
    //Hello obj=new Hello;
    System.out.println(Obj instanceof String);
    System.out.println(Obj instanceof Hello);
    System.out.println(Obj instanceof Object);
    Hello h=null;
    System.out.println(h instanceof Hello);
    System.out.println(h instanceof Object);
    }
    }  
    

提交回复
热议问题