When does the Java instanceof use?

Source: Internet
Author: User

1 when you get a reference to an object (such as a parameter), you may want to determine which class the reference really points to. So you need to start at the bottom of the tree from which the class inherits,
Using the instanceof operator, the first class that evaluates to True is the class that the reference really points to. 2 classperson{}3 classStudentextendsperson{}4 classPostgraduateextendsstudent{}5 classanimal{}6 Public classInstanceoftester {7 Public Static voidMain (string[] args) {8Instanceoftest (NewStudent ());9 }Ten Public Static voidinstanceoftest (person p) { One //determine the True type of P A if(pinstanceofpostgraduate) { -System.out.println ("P is an instance of class postgraduate"); -}Else if(pinstanceofStudent) { theSystem.out.println ("P is an instance of class student"); -}Else if(pinstanceofPerson ) { -System.out.println ("P is an instance of a class person"); -}Else if(pinstanceofObject) { +System.out.println ("P is an instance of class object"); - } + /*if (P instanceof Animal) {//This wrong compilation error, so make comments A System.out.println ("P is an example of Class animal"); at }*/ - } - } - - The output of this program is: P is an instance of class student - inThe inheritance tree where the person class resides is: object<--person<--student<--Postgraduate. - toThis example also joins a animal class, which is not in the tree of the person class, so it cannot be used as the right operand of the instanceof.

This demo is wrong.

1.if...else If the previous condition is satisfied, it will not continue to execute, even if the latter is true, so the above output will only have one.

But if you change the judgment all to if, take the above as an example, it will output: P is the instance of class student P is an instance of class person

P is an instance of the class object. As long as the if judgment satisfies the condition to carry on, does not have the order of precedence.

2. I just want to say that the author's statement is wrong, p is a reference, the new + construction method is an instance, can also be called an object

3.instanceof is a Java two-dollar operator, and ==,>,< is the same kind of stuff. Because it is made up of letters, it is also a reserved keyword for java.

Its purpose is to test whether the object on its left is an instance of the class to the right of it, returning a Boolean type of data.

Http://rodney.cnblogs.com/archive/2005/08/18/instanceof.html

When does the Java instanceof use?

Related Article

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.