Java Grammar Java did not learn, do not learn Android!

Source: Internet
Author: User

Int... A inside the ... Represents a mutable parameter, which means that this is an array of indefinite length

instanceof:

The instanceof keyword is used to determine whether an object that a reference type variable points to is an instance of a class (or interface, abstract class, parent class). As an example: PublicInterfaceIobject {
}

PublicclassFooImplementsiobject{
}

PublicclassTestextendsfoo{
}

Publicclassmultistatetest {
PublicStaticvoidMain (String args[]) {
Test ();
}

PublicStaticvoidTest () {
Iobject f=NewTest ();
if(finstanceofJava.lang.Object) System.out.println ("true");
if(finstanceofFoo) System.out.println ("true");
if(finstanceofTest) System.out.println ("true");
if(finstanceofIobject) System.out.println ("true");
}
} Output:true
true
true
trueIn addition, array types can be compared using instanceof. such as String str[] = new string[2], then str instanceof string[] will return true. 2>

Java's instanceof

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 and use the instanceof operator to judge that the first class that evaluates to TRUE is the one that the reference really points to.

For example, the following example:

Class person{}
Class Student extends person{}
Class Postgraduate extends student{}
Class animal{}
public class Instanceoftester {
public static void Main (string[] args) {
Instanceoftest (New Student ());
}
public static void Instanceoftest (person p) {
Determine the True type of P
if (P instanceof postgraduate) {
System.out.println ("P is an example of class postgraduate");
} else if (P instanceof Student) {
System.out.println ("P is an example of class student");
} else if (p instanceof person) {
System.out.println ("P is an instance of a class person");
} else if (P instanceof Object) {
System.out.println ("P is an instance of class object");
}
/*if (P instanceof Animal) {//This wrong compilation error, so make comments
System.out.println ("P is an example of Class animal");
}*/
}
}

The output of this program is: P is an instance of class student

The inheritance tree where the person class resides is: Object<--person<--student<--postgraduate.

This 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.

Java Grammar Java did not learn, do not learn Android!

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.