Example code for equivalence between instanceof and Class in java

  • 2021-01-06 00:36:04
  • OfStack

This paper mainly studies the equivalence of instanceof and Class in java, as follows.

The instanceof operator in java is used at runtime to indicate whether an object is an instance of a particular class. ES8en indicates whether the object is an instance of this particular class or one of its subclasses by returning a Boolean value.

Example 1 (instanceof)

Interface Person


public interface Person {
public void eat();
}

The implementation class People


public class People implements Person {
private int a=0;
 @Override
 public void eat() {
 System.out.println("======"+a);
 
 }

}

Subclasses xiaoming:


public class xiaoming extends People {
private String name;

@Override
public void eat() {
 System.out.println("+++++++++");
}
}

The 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 two above will compile without errors.

Operation results:


true
false
true
true

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("-----------------------------------------");
		System.out.println("x instanceof Base " + (x instanceof Base));
		System.out.println("x instanceof Derived " + (x instanceof Derived));
		System.out.println("-----------------------------------------");
		System.out.println("Base.isInstance(x) " + Base.class.isInstance(x));
		System.out.println("Derived.isInstance(x) " +
		        Derived.class.isInstance(x));
		System.out.println("-----------------------------------------");
		System.out.println("x.getClass() == Base.class " +
		        (x.getClass() == Base.class));
		System.out.println("x.getClass() == Derived.class " +
		        (x.getClass() == Derived.class));
		System.out.println("x.getClass().equals(Base.class)) " +
		        (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 is as follows:


Testing x of type Base
-----------------------------------------
x instanceof Base true
x instanceof Derived false
-----------------------------------------
Base.isInstance(x) true
Derived.isInstance(x) false
-----------------------------------------
x.getClass() == Base.class true
x.getClass() == Derived.class false
x.getClass().equals(Base.class)) true
x.getClass().equals(Derived.class)) false
*****************************************
*****************************************
Testing x of type Derived
-----------------------------------------
x instanceof Base true
x instanceof Derived true
-----------------------------------------
Base.isInstance(x) true
Derived.isInstance(x) true
-----------------------------------------
x.getClass() == Base.class false
x.getClass() == Derived.class true
x.getClass().equals(Base.class)) false
x.getClass().equals(Derived.class)) true
*****************************************
*****************************************

Process finished with exit code 0

Through the above tests, the following conclusions can be drawn:

The results generated by instanceof() and isInstance() are identical equals() and == produce the same result A superclass may be an instance of a subclass, but a subclass may not be an instance of a superclass Inheritance is not considered when Class objects are compared

conclusion

This article is about instanceof and Class equivalence code examples in java all content, hope to help you. Interested friends can continue to refer to the site of other related topics, if there are shortcomings, welcome to leave a message to point out. Thank you for your support!


Related articles: