Usage of instanceof in Java

Source: Internet
Author: User

The instanceof operator in Java is used to indicate at run time whether an object is an instance of a particular class. Instanceof is indicated by returning a Boolean value,

Whether this object is an instance of this particular class or its subclasses. The previous operand of the instanceof operation is usually a reference type, and the latter

The operand is usually a class (which can be an interface). He is used to determine whether the preceding object is a later class, or a subclass, to implement an instance object of the class, with a +,-etc

The arithmetic operator usage is roughly the same.

Examples are as follows:

 Public classInstanceoftest { Public Static voidMain (string[] agrs) {Object o= "Hello"; //Output TrueSystem.out.println ("Whether the string is an object instance:" +oinstanceofObject); //Output TrueSystem.out.println ("is string instance:" +oinstanceofString); String s= "World"; //compilation fails, string and math have no inheritance relationshipSystem.out.println ("Whether the string is a math instance:" +sinstanceofMath); }}

Usage of instanceof in Java

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.