用法:
result = object instanceof class
參數(shù):
result
必選項(xiàng)。任意變量。
object
必選項(xiàng)。任意對(duì)象表達(dá)式。
class
必選項(xiàng)。任意已定義的對(duì)象類。
說(shuō)明:
如果 object 是 class 的一個(gè)實(shí)例,則 instanceof 運(yùn)算符返回 true。如果 object 不是指定類的一個(gè)實(shí)例,或者 object 是 null,則返回 false。
例如:
Boolean b;
String str = "foo";
b = ( str instanceof String ); // true
b = ( str instanceof Object ); // also true
b = ( str instanceof Date ); // false, not a Date or subclass
注意:
1)null值不是任何對(duì)象的實(shí)例,所以下面這個(gè)例子返回了false,無(wú)論這個(gè)變量聲明的是什么類型。
String s = null;
if ( s instanceof String )
// false, won't happen
2)instanceof也可以正確的報(bào)告一個(gè)對(duì)象是否是數(shù)組和特定的接口類型。
if ( foo instanceof byte[] )