Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the instanceof keyword in Java

2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use the instanceof keyword in Java. It is very detailed and has a certain reference value. Friends who are interested must finish it!

Instanceof is strictly a binocular operator in Java to test whether an object is an instance of a class, using:

Boolean result = obj instanceof Class

Where obj is an object, and Class represents a class or an interface. When obj is the object of Class, or its direct or indirect subclass, or the implementation class of its interface, the result result returns true, otherwise it returns false.

Note: the compiler will check whether obj can be converted to the class type on the right. If it cannot be converted, the error will be reported directly. If the type is not determined, it will be compiled, depending on the runtime.

Instanceof

Instanceof is a reserved keyword for Java, with objects on the left and classes on the right, and the return type is of type Boolean. Its specific function is to test whether the object on the left is an instance object created by the right class or a subclass of that class. If so, it returns true, otherwise it returns false.

Matters needing attention in using instanceof

First there is inheritance relationship, then there is the use of instanceof.

When the test object is created, either the declaration type on the right and any class on the left must be the same branch of the inheritance tree or have an inheritance relationship with the test class, otherwise the compiler will report an error.

Instanceof uses the example public class Application {public static void main (String [] args) {/ / Object > Person > teacher / / Object > Person > Student / / Object > String Object o = new Student (); / / depending on the type of the object and the instantiated class name / / instanceof keyword, you can determine whether the object on the left is an instance System.out.println (o instanceof Student) of the right class or subclass. / / o is an instance object of the Student class, so determine whether the right class is related to student and display the declaration System.out.println (o instanceof Person); / / true System.out.println (o instanceof Object); / / true System.out.println (o instanceof String); / / false System.out.println (o instanceof Teacher); / / irrelevant System.out.println ("= ="); Person person = new Student () System.out.println (person instanceof Person); / / true System.out.println (person instanceof Object); / / true / / System.out.println (person instanceof String); / / compilation error System.out.println (person instanceof Teacher); / / No relationship}} instanceof application scenario

When you need to cast an object, you need to use instanceof to determine.

The above is all the content of the article "how to use the instanceof keyword in Java". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report