This article mainly studies the equivalence of instanceof and Class in Java, as follows.
The instanceof operator in java is used to indicate whether an object is an instance of a specific class at runtime. instanceof indicates whether the object is an instance of this particular class or a subclass by returning a boolean value.
Example 1 (instanceof)
Interface Person
public interface Person {public void eat();}Implement People class
public class People implements Person {private int a=0; @Override public void eat() { System.out.println("======="+a); }}Subclass xiaoming:
public class xiaoming extends People {private String name;@Overridepublic void eat() { System.out.println("++++++++++");}}Main function
public static void main(String[] args) { People p=new People(); xiaoming x=new xiaoming(); System.out.println(p instanceof Person); System.out.println(p instanceof xiaoming); -----2 System.out.println(x instanceof Person); System.out.println(x instanceof People); }Note: The code in the above 2 will not report errors during compilation.
Running results:
truefalsettrue
Example 2
package com.test.class_obj;class Base {}class Derived extends Base {}public class FamilyVsExactType {static void test(Object x) {System.out.println("Testing x of type " + x.getClass().getSimpleName());System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Base));System.out.println("x instanceof Derived " + (x instanceof Derived));System.out.println("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Derived.class.isInstance(x));System.out.println("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- (x.getClass().equals(Base.class)));System.out.println("x.getClass().equals(Derived.class)) " + (x.getClass().equals(Derived.class)));System.out.println("************************************************");System.out.println("******************************************************");}public static void main(String[] args) {test(new Base());test(new Derived());}}The output content is as follows:
Testing x of type Base------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- false*********************************************************************************Testing x of type Derived------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ Base.class falsesex.getClass() == Derived.class trueex.getClass().equals(Base.class)) falsesex.getClass().equals(Derived.class)) true********************************************************************************************************************************************************************************************************************************* Process finished with exit code 0
Through the above tests, the following conclusions can be drawn:
Summarize
The above is the entire content of this article about the equivalence code example of instanceof and Class in Java. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!