一.Java中关键字instanceof

  • 不仅可以判断对象是否是某个类的实例
  • 甚至连该类继承的基类和实现的接口也都能够被识别为true(实现接口的类也是接口的子类)

类定义:

1 public abstract class Number implements java.io.Serializable {}
2 public final class Integer extends Number implements Comparable<Integer> {}

测试:

1 public static void main(String args[]) {
 2     Object i = new Integer(7);
 3     if (i instanceof Number) {
 4         System.out.println("Integer i is a Number");
 5     } else {
 6         System.out.println("Integer i isn't a Number");
 7     }
 8 
 9     if (i instanceof Serializable) {
10         System.out.println("Integer i is a Serializable");
11     } else {
12         System.out.println("Integer i isn't a Serializable");
13     }
14 
15     if (i instanceof Integer) {
16         System.out.println("Integer i is an Integer");
17     } else {
18         System.out.println("Integer i isn't an Integer");
19     }
20 
21     if (i instanceof Float) {
22         System.out.println("Integer i is a Float");
23     } else {
24         System.out.println("Integer i isn't a Float");
25     }
26 }

结论:

1 Integer i is a Number
2 Integer i is a Serializable
3 Integer i is an Integer
4 Integer i isn't a Float

二.Class.equals

特征:

  • Java有一个叫做Class的类,这是一个用来描述类信息的类,我们如果要精确判断一个对象是否是具体的一个类的实例(必须是该类的实例否则都是false)

其他方法:

  • 获取父类的类型信息:getSuperclass()
  • 获取实现的接口的类型信息:getInterfaces()
  • 获取类型名称:getName()、getSimpleName()、getCanonicalName()

测试:

1 public static void main(String[] args) {
 2         Object i = new Integer(7);
 3 
 4         if (i.getClass().equals(Number.class)) {
 5             System.out.println("Integer i is a Number");
 6         } else {
 7             System.out.println("Integer i isn't a Number");
 8         }
 9 
10         if (i.getClass().equals(Serializable.class)) {
11             System.out.println("Integer i is a Serializable");
12         } else {
13             System.out.println("Integer i isn't a Serializable");
14         }
15 
16         if (i.getClass().equals(Integer.class)) {
17             System.out.println("Integer i is an Integer");
18         } else {
19             System.out.println("Integer i isn't an Integer");
20         }
21 
22         if (i.getClass().equals(Float.class)) {
23             System.out.println("Integer i is a Float");
24         } else {
25             System.out.println("Integer i isn't a Float");
26         }
27     }

结论:

1 Integer i isn't a Number
2 Integer i isn't a Serializable
3 Integer i is an Integer
4 Integer i isn't a Float

三.Class.isInstance

作用:这个方法跟instanceof完全等价

RunnableImpl类:

1 public class RunnableImpl implements Runnable {
2 
3     
4     @Override
5     public void run() {
6         
7     }
8 }

测试:

1 //判断RunnableImpl是Runnable的子类
2 System.out.println(Runnable.class.isInstance(new RunnableImpl()));
3 //判断RunnableImpl是RunnableImpl的子类
4 System.out.println(RunnableImpl.class.isInstance(new RunnableImpl()));
5 //判断Object是RunnableImpl的子类
6 System.out.println(RunnableImpl.class.isInstance(new Object()));

结论:

1 true
2 true
3 false

四.Class.isAssignableFrom

作用:判断某个类是否是另一个类和其子类。

测试:

1 //判断RunnableImpl是Runnable的子类
2 System.out.println(Runnable.class.isAssignableFrom(RunnableImpl.class));
3 //判断RunnableImpl是RunnableImpl的子类
4 System.out.println(RunnableImpl.class.isAssignableFrom(RunnableImpl.class));
5 //判断Object是RunnableImpl的子类
6 System.out.println(RunnableImpl.class.isInstance(Object.class));

结论:

1 true
2 true
3 false