instanceof 运算符是用来在运行时指出对象是不是特定
类的一个实例。instanceof经过返回一个布尔值来指出,这个对象是不是这个特定类或者是它的子类的一个实例。
用法:
result = object instanceof class
参数:
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也能够正确的报告一个对象是不是数组和特定的接口类型。 if ( foo instanceof byte[] )