The meaning and usage of instanceof

Source: Internet
Author: User

first, let's explain what instanceof is, Instanceof is a two-dollar operator (operator) of Java and PHP, and ==,>,< is the same kind of thing. Because it is made up of letters, it is also a reserved keyword for Java . Its function 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 instance of a subclass in an inheritance is an implementation of a parent class. Equivalent to the IS operator in C #. The instanceof operator in Java is used to indicate at run time whether an object is an instance of a particular class. Instanceof Indicates whether this object is an instance of this particular class or its subclasses by returning a Boolean value.

Next I will explain the meaning and usage of instanceof from four aspects:

First, usage

Boolean result = Object instanceof class

Second, the parameters

Result: Boolean type.

Object: Required option. An arbitrary object expression.

Class: Required option. Any of the defined object classes.

Third, the description

If object is an instance of class, the instanceof operator returns TRUE. Returns False if object is not an instance of the specified class, or if object is null.

Iv..

Package com.instanceoftest;

Interface a{}

Class B implements a{}

Class C extends B {}

Class Instanceoftest {

public static void Main (string[] args) {

A A=null;

B B=null;

Boolean res;

System.out.println ("Instanceoftest test Case 1:------------------");

res = a instanceof A;

System.out.println ("A instanceof A:" + res);
  
res = b instanceof B;
System.out.println ("b instanceof B:" + res);

System.out.println ("\ninstanceoftest test Case 2:------------------");

A=new B ();

B=new B ();

res = a instanceof A;

System.out.println ("A instanceof A:" + res);

res = a instanceof B;

System.out.println ("A instanceof B:" + res);

res = b instanceof A;

System.out.println ("b instanceof A:" + res);

res = b instanceof B;

System.out.println ("b instanceof B:" + res);

System.out.println ("\ninstanceoftest test Case 3:------------------");

b b2= (b) New C ();

res = B2 instanceof A;

System.out.println ("B2 instanceof A:" + res);

res = B2 instanceof B;

System.out.println ("B2 instanceof B:" + res);

res = B2 instanceof C;

System.out.println ("B2 instanceof C:" + res);

}
}


/*result:

Instanceoftest test Case 1:------------------

A instanceof A:false

b instanceof B:false

Instanceoftest test Case 2:------------------

A instanceof A:true

A instanceof B:true

b instanceof A:true

b instanceof B:true

Instanceoftest test Case 3:------------------

B2 instanceof A:true

B2 instanceof B:true

B2 instanceof C:true



This article is from the "focus on Development Technology" blog, please be sure to keep this source http://bingge2015.blog.51cto.com/4332222/1684876

The meaning and usage of instanceof

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.