instanceof关键字

//样例一
Object object = new Student();
System.out.println(object instanceof String) //false
//样例二
Student object = new Student();
System.out.println(object instanceof String) //编译错误

为什么样例一和样例二报错的原因不一样
因为在编译的时候只看数据类型,即左边的数据类型
样例一中左边的数据类型是Object,因此(String)object可以编译通过,但是具体object的右侧是什么(即他的实例化对象)要等运行时才知道,因此运行时比较的是右侧
样例二中左边的数据类型是Student,因此(String)object无法编译通过

instanceof关键字

上一篇:CF1388E Uncle Bogdan and Projections 题解


下一篇:leetcode 字母异位词分组 中等