instanceof运算符
来源:互联网 发布:cctv5直播软件 编辑:程序博客网 时间:2024/06/06 06:44
一 instanceof运算符两个注意点
- 编译时,instanceof运算符前面操作数的编译时类型要么与后面类相同,要么与后面类具有父子继承关系。
- instanceof前一个操作数通常是一个引用类型变量,后面一个操作数通常是一个类(接口),它用于判断前面对象是否是后面的类,或者其子类、实现类的实例。如果是,则返回true,否则返回false。
二instanceof应用
1 代码示例
public class InstanceofTest{public static void main(String[] args){// 声明hello时使用Object类,则hello的编译类型是Object,// Object是所有类的父类, 但hello变量的实际类型是StringObject hello = "Hello";// String与Object类存在继承关系,可以进行instanceof运算。返回true。System.out.println("字符串是否是Object类的实例:"+ (hello instanceof Object));System.out.println("字符串是否是String类的实例:"+ (hello instanceof String)); // 返回true。// Math与Object类存在继承关系,可以进行instanceof运算。返回false。System.out.println("字符串是否是Math类的实例:"+ (hello instanceof Math));// String实现了Comparable接口,所以返回true。System.out.println("字符串是否是Comparable接口的实例:"+ (hello instanceof Comparable));String a = "Hello";//// String类与Math类没有继承关系,所以下面代码编译无法通过//System.out.println("字符串是否是Math类的实例:"//+ (a instanceof Math));}}
2 运行结果
字符串是否是Object类的实例:true
字符串是否是String类的实例:true
字符串是否是Math类的实例:false
字符串是否是Comparable接口的实例:true
0 0
- instanceof 运算符
- instanceof运算符
- Instanceof运算符
- instanceof 运算符 小结
- instanceof 运算符
- instanceof 运算符
- instanceof 运算符 笔记
- instanceof运算符
- android instanceof 运算符
- instanceof运算符
- instanceof typeof 运算符
- instanceof 运算符
- instanceof typeof 运算符
- instanceof运算符
- instanceof运算符
- instanceof运算符
- JAVA运算符 instanceof
- php-instanceof运算符
- shell 删除jar包中的文件或文件夹
- 深入理解C++枚举类型enum
- 原型模式应用——展现每日生活
- Java中的super限定
- 关于wamp+eclipse for PHP环境的搭建以及其中部分问题的解决办法
- instanceof运算符
- 强制类型转换讨论
- Java多态性
- 子类构造器调用父类构造器的顺序问题
- Linux命令'门'
- 使用sencha cmd创建项目
- 初始化块
- qt5.3.2移植smart210
- Linux下Segmentation fault(core dumped)简单调试方法