java instanceof運(yùn)算符用于測(cè)試指定對(duì)象是否是指定類型(類或子類或接口)的實(shí)例。
java中的instanceof也稱為類型比較運(yùn)算符,因?yàn)樗鼘㈩愋团c實(shí)例進(jìn)行比較。 它返回true或false。 如果對(duì)任何具有null值的變量應(yīng)用instanceof運(yùn)算符,則返回false。
java instanceof的簡(jiǎn)單示例
下面來看看實(shí)例運(yùn)算符的簡(jiǎn)單示例,它測(cè)試當(dāng)前類。
class Simple1 {
public static void main(String args[]) {
Simple1 s = new Simple1();
System.out.println(s instanceof Simple1);// true
}
}
執(zhí)行上面試代碼,得到以下結(jié)果 -
true
子類類型的對(duì)象也是父類的類型。 例如,如果Dog擴(kuò)展了Animal,那么Dog的對(duì)象可以通過Dog或Animal類來引用。
java instanceof運(yùn)算符的另一個(gè)例子
class Animal {
}
class Dog1 extends Animal {// Dog inherits Animal
public static void main(String args[]) {
Dog1 d = new Dog1();
System.out.println(d instanceof Animal);// true
}
}
執(zhí)行上面代碼,得到以下結(jié)果 -
true
instanceof測(cè)試null值的變量示例
如果我們對(duì)具有null值的變量應(yīng)用instanceof運(yùn)算符,則返回false。來看看下面給出的例子,將instanceof運(yùn)算符應(yīng)用于具有null值的變量。
class Dog2 {
public static void main(String args[]) {
Dog2 d = null;
System.out.println(d instanceof Dog2);// false
}
}
執(zhí)行上面代碼,得到以下結(jié)果 -
false
使用java instanceof運(yùn)算符的向下轉(zhuǎn)換
當(dāng)子類型引用父類的對(duì)象時(shí),它被稱為向下轉(zhuǎn)換(downcasting)。 如果直接執(zhí)行它,編譯器會(huì)出現(xiàn)編譯錯(cuò)誤。 如果通過類型轉(zhuǎn)換來執(zhí)行,在運(yùn)行時(shí)會(huì)拋出:ClassCastException 。 但是如果使用instanceof運(yùn)算符,可以進(jìn)行向下轉(zhuǎn)換。
Dog d=new Animal();//Compilation error
如果通過類型轉(zhuǎn)換執(zhí)行向下轉(zhuǎn)換,則在運(yùn)行時(shí)拋出:ClassCastException。
Dog d=(Dog)new Animal();
//Compiles successfully but ClassCastException is thrown at runtime
使用instanceof進(jìn)行向下轉(zhuǎn)換
現(xiàn)在看看下面這個(gè)例子,通過instanceof運(yùn)算符進(jìn)行向下轉(zhuǎn)換。
class Animal {
}
class Dog3 extends Animal {
static void method(Animal a) {
if (a instanceof Dog3) {
Dog3 d = (Dog3) a;// downcasting
System.out.println("ok downcasting performed");
}
}
public static void main(String[] args) {
Animal a = new Dog3();
Dog3.method(a);
}
}
執(zhí)行上面代碼,得到以下結(jié)果 -
ok downcasting performed
向下轉(zhuǎn)換不使用instanceof
也可以在不使用instanceof運(yùn)算符的情況下執(zhí)行下轉(zhuǎn)換,如以下示例代碼所示:
class Animal {
}
class Dog4 extends Animal {
static void method(Animal a) {
Dog4 d = (Dog4) a;// downcasting
System.out.println("ok downcasting performed");
}
public static void main(String[] args) {
Animal a = new Dog4();
Dog4.method(a);
}
}
執(zhí)行上面代碼,得到以下結(jié)果 -
ok downcasting performed
仔細(xì)看看,被引用的實(shí)際對(duì)象是Dog類的對(duì)象。 所以如果向下轉(zhuǎn)換它,它是沒有問題的。 但是,如果也可以這樣寫:
Animal a=new Animal();
Dog.method(a);
//Now ClassCastException but not in case of instanceof operator
這是一個(gè)instanceof的終極示例,通過下面的例子中的代碼看看instanceof關(guān)鍵字的真正用法。
interface Printable {
}
class A implements Printable {
public void a() {
System.out.println("a method");
}
}
class B implements Printable {
public void b() {
System.out.println("b method");
}
}
class Call {
void invoke(Printable p) {// upcasting
if (p instanceof A) {
A a = (A) p;// Downcasting
a.a();
}
if (p instanceof B) {
B b = (B) p;// Downcasting
b.b();
}
}
}// end of Call class
class Test4 {
public static void main(String args[]) {
Printable p = new B();
Call c = new Call();
c.invoke(p);
}
}
執(zhí)行上面代碼,得到以下結(jié)果 -
b method