用法:
result = object instanceof class
參數(shù):
result
必選項。任意變量。
object
必選項。任意對象表達式。
class
必選項。任意已定義的對象類。
說明:
如果 object 是 class 的一個實例,則 instanceof 運算符返回 true。如果 object 不是指定類的一個實例,或者 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值不是任何對象的實例,所以下面這個例子返回了false,無論這個變量聲明的是什么類型。
String s = null;
if ( s instanceof String )
// false, won't happen
2)instanceof也可以正確的報告一個對象是否是數(shù)組和特定的接口類型。
if ( foo instanceof byte[] )