The Type Comparison Operator instanceof
来源:互联网 发布:java jni 参数 编辑:程序博客网 时间:2024/06/06 02:16
This piece of writing is reprinted from the following URL:
http://download.oracle.com/javase/tutorial/java/nutsandbolts/op2.html
The instanceof
operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.
The following program, InstanceofDemo
, defines a parent class (named Parent
), a simple interface (named MyInterface
), and a child class (named Child
) that inherits from the parent and implements the interface.
class InstanceofDemo { public static void main(String[] args) { Parent obj1 = new Parent(); Parent obj2 = new Child(); System.out.println("obj1 instanceof Parent: " + (obj1 instanceof Parent)); System.out.println("obj1 instanceof Child: " + (obj1 instanceof Child)); System.out.println("obj1 instanceof MyInterface: " + (obj1 instanceof MyInterface)); System.out.println("obj2 instanceof Parent: " + (obj2 instanceof Parent)); System.out.println("obj2 instanceof Child: " + (obj2 instanceof Child)); System.out.println("obj2 instanceof MyInterface: " + (obj2 instanceof MyInterface)); }}class Parent{}class Child extends Parent implements MyInterface{}interface MyInterface{}
obj1 instanceof Parent: trueobj1 instanceof Child: falseobj1 instanceof MyInterface: falseobj2 instanceof Parent: trueobj2 instanceof Child: trueobj2 instanceof MyInterface: true
instanceof
operator, keep in mind that null
is not an instance of anything.
- The Type Comparison Operator instanceof
- The Type Comparison Operator instanceof
- Comparison operator in shell
- KSH comparison operator
- operator type()
- Check whether two input intergers are the same without using comparison operator
- type-cast operator
- The operator == is undefined for the argument type(s) int, null
- The operator || is undefined for the argument type(s) int, int
- 学习type of和instanceof
- The Abstract Equality Comparison Algorithm
- The instanceof Keyword
- overload the operator
- Explaining the PeopleCode @ operator
- qmake "The *= operator"
- perl the special operator
- The sizeof Operator
- The in operator
- android 输入框 XML 设置于代码设置异同
- loadrunner 录制winsocket协议经验总结
- mac系统下eclipse搭建android环境配置的几点小问题
- pku2536 Gopher II
- 自定义tooltip
- The Type Comparison Operator instanceof
- 究竟是社会不公平,还是我们太浮躁?
- 收缩SQL SERVER 2008日志文件
- MyEclipse Database Explorer 连接oracle 数据库连不上
- 面试之技术面试篇
- gdiplus.h的引用
- DataTable.Compute方法使用实例
- 一个男人关心的东西 决定了他的层次
- 公司核心竞争力是什么