原文: https://www.programiz.com/java-programming/instanceof

在本教程中,您将在示例的帮助下详细了解 Java instanceof运算符。

在 Java 中,instanceof关键字是二进制运算符。 它用于检查对象是否是特定类的实例。

运算符还检查对象是否是实现接口的类的实例(将在本教程的后面进行讨论)。

instanceof的语法为:

  1. result = objectName instanceof className;

instanceof运算符的左操作数是对象名称,右操作数是类名称。 如果对象是类的实例,则结果为true,否则为false


示例 1:instanceof

  1. class Main {
  2. public static void main (String[] args) {
  3. String name = "Programiz";
  4. Integer age = 22;
  5. System.out.println("Is name an instance of String: "+ (name instanceof String));
  6. System.out.println("Is age an instance of Integer: "+ (age instanceof Integer));
  7. }
  8. }

输出

  1. Is name an instance of String: true
  2. Is age an instance of Integer: true

在上面的示例中,我们创建了String类型的对象nameInteger类型的另一个对象age。 然后,我们使用instanceof运算符检查name是否为String类型,以及age是否为Integer类型。


在继承中使用instanceof

在继承的情况下,instanceof运算符用于检查子类的对象是否也是超类的实例。

示例 2:继承中的instanceof

  1. class Animal {
  2. }
  3. // Dog class is a subclass of Animal
  4. class Dog extends Animal {
  5. }
  6. class Main {
  7. public static void main(String[] args){
  8. Dog d1 = new Dog();
  9. // checks if d1 is an object of Dog
  10. System.out.println("Is d1 an instance of Dog: "+ (d1 instanceof Dog));
  11. // checks if d1 is an object of Animal
  12. System.out.println("Is d1 an instance of Animal: "+ (d1 instanceof Animal));
  13. }
  14. }

输出

  1. Is d1 is an instance of Dog: true
  2. Is d1 an instance of Animal: true

在上面的示例中,d1DogAnimal类的实例。 因此,d1 instanceof Dogd1 instanceof Animal都导致true


Object

在 Java 中,所有类均继承自Object类。 在Object类的继承期间,不使用extends关键字。 这种继承隐式发生在 Java 中。

示例 3:Object

  1. class Animal {
  2. }
  3. class Dog {
  4. }
  5. class Cat {
  6. }
  7. class Main {
  8. public static void main(String[] args) {
  9. Dog d1 = new Dog();
  10. Animal a1 = new Animal();
  11. Cat c1 = new Cat();
  12. System.out.println("Is d1 an instance of the Object class: "+ (d1 instanceof Object));
  13. System.out.println("Is a1 an instance of the Object class: "+ (a1 instanceof Object));
  14. System.out.println("Is c1 an instance of the Object class: "+ (c1 instanceof Object));
  15. }
  16. }

输出

  1. Is d1 an instance of the Object class: true
  2. Is a1 an instance of the Object class: true
  3. Is c1 an instance of the Object class: true

在上述示例中,我们创建了Animal a1Dog类的d1Cat c1。 我们已经使用instanceof运算符来检查这些对象a1d1c1是否也是Object类的对象 。 全部输出结果为true

这是因为Object类是java.lang包中定义的根类。 所有其他类都是Object类的子类,在 Java 中形成层次结构。


对象向上转换和向下转换

在 Java 中,子类的对象可以视为超类的对象。 这称为向上转换。

Java 编译器自动执行向上转换。

示例 4:对象向上转换

  1. class Animal {
  2. public void displayInfo() {
  3. System.out.println("I am an animal.");
  4. }
  5. }
  6. class Dog extends Animal {
  7. }
  8. class Main {
  9. public static void main(String[] args) {
  10. Dog d1 = new Dog();
  11. Animal a1 = d1;
  12. a1.displayInfo();
  13. }
  14. }

输出

  1. I am an animal.

在上面的示例中,我们创建了Dog类的对象d1。 我们使用d1对象创建Animal类的对象a1。 在 Java 中,这称为向上转换。

该代码执行没有任何问题。 这是因为上载是由 Java 编译器自动完成的。

向下转换是向上转换的相反过程。

在向下转换的情况下,超类的对象被视为子类的对象。 我们必须明确指示编译器使用 Java 下调。


示例 5:对象向下转换问题

  1. class Animal {
  2. }
  3. class Dog extends Animal {
  4. public void displayInfo() {
  5. System.out.println("I am a dog.");
  6. }
  7. }
  8. class Main {
  9. public static void main(String[] args) {
  10. Animal a1 = new Animal();
  11. Dog d1 = (Dog)a1; // Downcasting
  12. d1.displayInfo();
  13. }
  14. }

运行程序时,将获得名为ClassCastException的异常。 让我们看看这里发生了什么。

在这里,我们创建了超类Animal的对象a1。 然后,我们尝试将a1对象转换为子类Dog的对象d1

这引起了问题。 这是因为超类Animala1对象也可能引用其他子类。 如果我们创建了另一个子类CatDogAnimal可能是Cat,也可能是Dog引起歧义。

要解决此问题,我们可以使用instanceof运算符。 这是如何做:


示例 6:使用instanceof解决向下转换

  1. class Animal {
  2. }
  3. class Dog extends Animal {
  4. public void displayInfo() {
  5. System.out.println("I am a dog");
  6. }
  7. }
  8. class Main {
  9. public static void main(String[] args) {
  10. Dog d1 = new Dog();
  11. Animal a1 = d1; // Upcasting
  12. if (a1 instanceof Dog){
  13. Dog d2 = (Dog)a1; // Downcasting
  14. d2.displayInfo();
  15. }
  16. }
  17. }

输出

  1. I am a dog

在上面的示例中,我们使用instanceof运算符检查a1对象是否是Dog类的实例。 仅当表达式a1 instanceof Dogtrue时才进行向下转换。


接口中的instanceof

instanceof运算符还用于检查类的对象是否也是实现该类的接口的实例。

示例 7:接口中的instanceof

  1. interface Animal {
  2. }
  3. class Dog implements Animal {
  4. }
  5. class Main {
  6. public static void main(String[] args) {
  7. Dog d1 = new Dog();
  8. System.out.println("Is d1 an instance of Animal: "+(d1 instanceof Animal));
  9. }
  10. }

输出

  1. Is d1 an instance of Animal: true

在上面的示例中,我们创建了一个Dog类,该类实现了Animal接口。

然后,创建Dog类的d1对象。 我们已经使用instanceof运算符检查d1对象是否也是Animal接口的实例。