http://lavasoft.blog.51cto.com/62575/79864/Deep java keyword instanceof2008-06-02 07:50:43Tags: Java keywords casual workplace instanceof original works, allow reprint, please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise, the legal liability will be investigated. http://lavasoft.blog.51cto.com/62575/79864 in-depth java keyword instanceof instanceof keyword is used to determine whether a reference type variable points to an object that is a class (or interface, abstract class, The parent Class) instance. 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.
This article is from the lava blog, so be sure to keep this source http://lavasoft.blog.51cto.com/62575/79864
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).