Introduction to Java for beginners 200 examples 89 Java instanceof keyword

Keywords: Java

Introduction to the author

Author name: Ming Shiyin in programming world
Introduction: CSDN blog expert has been engaged in software development for many years and is proficient in Java and JavaScript. Bloggers also learn and grow step by step from scratch, know the importance of learning and accumulation, and like to fight and upgrade with ADC. Welcome to pay attention and look forward to learning, growing and taking off with you!

introduction

Many Java beginners ask me that it's worrying for the novice to turn around and forget the Java knowledge he has studied very hard. How can Xiaobai grow up quickly and become a big cow?
In fact, there is only one skill to become a big God: "learn more and practice more", so brother Ming sorted out typical practice examples. Through practice, you can quickly improve coding skills and proficiency, so that you can never return on the way to become a big man (remember to practice with your own hands)!

Navigation

✪ introduction to Java white 200 case series directory index
◄ previous article    88.Java polymorphism
► next article to be updated     

summary

instanceof is a reserved keyword for Java. Its function is to test whether the object on its left is an instance of the class on its right, and return the boolean data type.

Syntax format:

boolean result = obj instanceof Class

Where obj is an object and class represents a class or interface. When obj is an instance or subclass instance of a class (or interface), the result returns true; otherwise, it returns false.

Example 1

obj must be a reference type, not a basic type, otherwise / compilation fails.

package demo.demo89;

public class Instanceof {

	public static void main(String[] args) {
		int i = 0;
		System.out.println(i instanceof Integer);//Compilation failed
	}
}

Example 2

When obj is null, it directly returns false because null does not refer to any object.

package demo.demo89;

public class Instanceof {

	public static void main(String[] args) {
		System.out.println(null instanceof Integer);
		System.out.println(null instanceof String);
		System.out.println(null instanceof Object);
	}
}

Operation results:

false
false
false

Example 3

obj is an instance object of Class and returns true.

package demo.demo89;

public class Student {
	private String name;

	public Student(String name){
		this.name=name;
	}
}
package demo.demo89;

public class Instanceof {

	public static void main(String[] args) {
		Student stu = new Student("cute girl"); 
		boolean b = stu instanceof Student;
		System.out.println(b);
	}
}

function:

true

Example 4

obj is the instance object of the implementation Class of the Class interface, and returns true.

package demo.demo89;
//Interface
public interface A {

}
package demo.demo89;
//Implementation class
public class B implements A {

}
package demo.demo89;

public class Test {

	public static void main(String[] args) {
		B b = new B();
		boolean bool = b instanceof A;
		System.out.println(bool);
	}
}

function:

true

Example 5

obj is a direct or indirect subclass of Class.

package demo.demo89;
//Parent class Person
public class Person {

}
package demo.demo89;
//Subclass Man of Person
public class Man extends Person{
	
}
package demo.demo89;
//Subclass of Man Child
public class Child extends Man {

}
package demo.demo89;

public class Test {

	public static void main(String[] args) {
		Man man = new Man();
		//Man is a direct subclass of Person
		System.out.println(man instanceof Person);
		Child child = new Child();
		//Child is a direct subclass of Man
		System.out.println(child instanceof Man);
		//Child is an indirect subclass (descendant) of Person
		System.out.println(child instanceof Person);
	}
}

Operation results:

true
true
true

If the direct subclass and indirect subclass are checked with instanceof, true will be returned.

other

The compiler will check whether obj can be converted to the class type on the right. If it cannot be converted, an error will be reported directly. If the type cannot be determined, it will be compiled.

package demo.demo89;

import java.util.List;

public class Test {

	public static void main(String[] args) {
		Man man = new Man();
		//Man is a direct subclass of Person
		System.out.println(man instanceof Person);
		
		//System.out.println(man instanceof String);    //  Compilation error
		System.out.println(man instanceof List);    // false
	}
}

The man instanceof List compiler cannot determine whether it is true or false. It can only return false when running.

Summary

This section summarizes the "Java instanceof keyword". I hope it can be helpful to you. Please help [like] + [collection] + [punch in the comment area]. If you are interested in learning java with brother Xiao Ming, [pay attention to a wave] won't get lost.

Let me know you by punching in the comment area. Mingge will continue to pay attention to your learning progress!

Navigation

✪ introduction to Java white 200 case series directory index
◄ previous article    88.Java polymorphism
► next article to be updated     

Popular column recommendation

1.Java game series (Tetris, aircraft war, plant war, zombie, etc.)
2.JavaWeb project practice (library management, online examination, dormitory management system, etc.)
3. Wonderful examples of JavaScript (aircraft war, minesweeping, snake eating, verification code, etc.)
4. Introduction to Java Xiaobai 200 cases
5. Learn Java from zero, learn Java with interest, and learn Java from the perspective of King glory

Posted by John_S on Tue, 05 Oct 2021 15:57:04 -0700