---
title: "instanceof 运算符"
author: "rainyear"
date: "2011-09-05T10:00:35Z"
tags: []
categories: ["非技术"]
slug: "instanceof"
draft: false
lastmod: "2011-09-05T10:00:35Z"
id: 1165569
url: "https://rainyear.iteye.com/blog/1165569"
---

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