Java instanceof operator __effective Java 2.0_item 8 knowledge Point __java

Source: Internet
Author: User

Article Author: Tyan
Blog: noahsnail.com |  CSDN | Jianshu

The instanceof operator is a two-dollar operator and is a reserved keyword in java. Its role is to determine whether its left object is an instance of its right class, and returns a Boolean type of data. Can be used to determine whether an object is an instance of a particular class or an instance of its subclass.

The use of instanceof is:

Boolean result = Object instanceof class

Its parameters are:

Result: Boolean type.

Object: Any expression of objects.

Class: Any defined object class.

Example:

Import java.util.ArrayList;
Import java.util.Collection;
Import java.util.List;

public class Test {public

    static void Main (string[] args) {
        String s  = ' abc ';
        System.out.println (S instanceof String);

        list<string> list = new arraylist<> ();
        SYSTEM.OUT.PRINTLN (list instanceof Collection);

        SYSTEM.OUT.PRINTLN (null instanceof String);
    }

Test results:

True
True
False

Note: The instanceof operator is primarily used to determine the type of object that is used by the Equals method of the object.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.