instanceof,
分享于 点击 22497 次 点评:155
instanceof,
1.instanceof
instanceof: 通过一个返回一个布尔值来判断这个对象是否是这个特定类、一个实现指定接口的类的实例或者是它的子类的一个实例。
实例代码1
package com.niuke;
public class Test {
public static void main(String[] args){
Integer i = new Integer(9) ;
boolean b = i instanceof Object;
System.out.print(b);
}
}
我们知道Object类是任何类的子类;所以应该返回true;
运行结果:
true
实例代码2
public class test{
public static void main(String[] args) {
List list = new ArrayList();
int a = 1;
list.add(a);//arrayList.add()需要的是Integer对象,所以把int类型自动装箱成Integer类型。
List list1 = list;
System.out.println(list.get(0) instanceof Integer);//true
System.out.println(list1.get(0) instanceof Integer);//true
}
}
相关文章
- 暂无相关文章
用户点评