java 中的instanceof用法详解及instanceof是什么意思(推荐)
作者:nextnj 发布时间:2023-06-07 13:52:27
好,应大家的要求先给大家说下在JAVA程序中instanceof是什么意思
instanceof是Java的一个二元操作符,和==,>,<是同一类东东。由于它是由字母组成的,所以也是Java的保留关键字。它的作用是测试它左边的对象是否是它右边的类的实例,返回boolean类型的数据。
instanceof运算符用法
运算符是双目运算符,左面的操作元是一个对象实例,右面是一个类.当左面的对象是右面的类创建的对象时,该运算符运算的结果是true,否则是false
说明:
(1).一个类的实例包括本身的实例,以及所有直接或间接子类的实例
(2).instanceof左边操作元显式声明的类型与右边操作元必须是同种类或有继承关系,
即位于继承树的同一个分支上,否则会编译出错
double obj=1;
if(obj instanceof Double){
System.out.println("true");
}
报 "Incompatible conditional operand types double and Double" 错误
obj 必须是对象的实例。不能是基础数据类型。
String obj=1.0+"";
if(obj instanceof Double){
System.out.println("true");
}
报 "Incompatible conditional operand types String and Double" 错误
String 和 Double 不是位于继承树的同一个分支上。
if(null instanceof Object){
System.out.println("true");
}else{
System.out.println("false");
}
String obj=null;
if(obj instanceof Object){
System.out.println("true");
}else{
System.out.println("false");
}
打印都为 false. null用操作符instanceof测试任何类型时都是返回false的。
if(obj instanceof null){
System.out.println("true");
}else{
System.out.println("false");
}
编译出错。报"Syntax error on token "null", invalid ReferenceType" 错误。
public class Test {
public static void main(String[] args){
System.out.println(new Student() instanceof String); //compile time error
System.out.println(new Student() instanceof Exception); //compile time error
System.out.println(new Student() instanceof Object); //compilation and output true
System.out.println(new Student() instanceof List); //compilation and output false
System.out.println(new Student() instanceof List<?>); //compilation and output false
System.out.println(new Student() instanceof List<String>); //compile time error
System.out.println(new Student() instanceof List<Object>); //compile time error
System.out.println(new String() instanceof List); //compile time error
System.out.println(new String() instanceof List<?>); //compile time error
System.out.println(null instanceof Object); //compilation and output false
}
}
class Student{
}
看到上面的测试结果可能会有这样那样的疑惑,为什么Student对象测试String的时候是编译错误,而测试List的时候又能够通过(难道是instanceof对接口在编译时不作检查呢,还是由于List类型本身在编译时不确定具体类型导致的),但是后面你又会发现如果是List<String>的时候编译就不通过了(看来前面的猜测是错误的,他对接口是要做检查的),可是往后面看,你又会纳闷String测试List还是List<?>的时候,直接在编译期就报错了,这跟Student对象测试List和List<?>的结果大不同,可能此时你有点相当不解了。我们这个时候翻看java对instanceof操作符的说明时,发现instanceof的这些表现都是跟cast操作符是有点关系的,就是说当我们在instanceof方法时,编译器会检查这个对象能够cast到右边的类型,如果不能cast则直接报错,如果不能确定类型,则通过编译,具体看运行时定。
这里可能还有一个疑惑,我们Student已经确定类型了啊,List类型也是确定的啊,为什么能够编译通过呢,而String却不行呢(难道是自己定义的类和系统定义的类在编译处理上有不同),这里java没有做特别的说明,但是我想可能是因为final关键字的区别造成的,我们发现不论String、Integer、Long等都是最终类,他们的处理类似于编译器对常量的处理,因为编译器知道这个类在运行期间是不会有改变的,故而编译器在编译期间认为他自己能够确定这个类的最终类型。而对于自己定义的类呢(我试过系统的非最终类Hashtable、HashMap等,测试和我们自定义的类的结果一样,可以通过编译),由于不是最终类,可能编译器认为他可能在运行期间会有改变的可能,故而不把他作最为确定的类型处理,故而可以通过编译。其实当我们在自定义的类前面加上final关键字的时候,其表现就跟String、Integer、Long这些最终类测试instanceof表现的一样了。
好,下面通过实例代码看下java中instanceof用法
java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例。instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例。
用法:
result = object instanceof class
参数:
Result:布尔类型。
Object:必选项。任意对象表达式。
Class:必选项。任意已定义的对象类。
说明:
如果 object 是 class 的一个实例,则 instanceof 运算符返回 true。如果 object 不是指定类的一个实例,或者 object 是 null,则返回 false。
示例代码如下:
package com.instanceoftest;
interface A{}
class B implements A{
}
class C extends B {
}
class instanceoftest {
public static void main(String[] args){
A a=null;
B b=null;
boolean res;
System.out.println("instanceoftest test case 1: ------------------");
res = a instanceof A;
System.out.println("a instanceof A: " + res);
res = b instanceof B;
System.out.println("b instanceof B: " + res);
System.out.println("/ninstanceoftest test case 2: ------------------");
a=new B();
b=new B();
res = a instanceof A;
System.out.println("a instanceof A: " + res);
res = a instanceof B;
System.out.println("a instanceof B: " + res);
res = b instanceof A;
System.out.println("b instanceof A: " + res);
res = b instanceof B;
System.out.println("b instanceof B: " + res);
System.out.println("/ninstanceoftest test case 3: ------------------");
B b2=(C)new C();
res = b2 instanceof A;
System.out.println("b2 instanceof A: " + res);
res = b2 instanceof B;
System.out.println("b2 instanceof B: " + res);
res = b2 instanceof C;
System.out.println("b2 instanceof C: " + res);
}
}
/*
result:
instanceoftest test case 1: ------------------
a instanceof A: false
b instanceof B: false
instanceoftest test case 2: ------------------
a instanceof A: true
a instanceof B: true
b instanceof A: true
b instanceof B: true
instanceoftest test case 3: ------------------
b2 instanceof A: true
b2 instanceof B: true
b2 instanceof C: true
*/
总结
以上所述是小编给大家介绍的java 中的instanceof用法详解及instanceof是什么意思网站的支持!
来源:https://www.cnblogs.com/nextnj/archive/2013/05/30/3107984.html


猜你喜欢
- 简介#要说Java中什么异常最容易出现,我想NullPointerException一定当仁不让,为了解决这种null值判断问题,Java8
- 详解Kotlin Android开发中的环境配置在Android Studio上面进行安装插件在Settings ->Plugins
- 【前言】面向资源的 Restful 风格的 api 接口本着简洁,资源,便于扩展,便于理解等等各项优势,在如今的系统服务中越来越受欢迎。.n
- 本文实例讲述了Android之复选框对话框用法。分享给大家供大家参考。具体如下:main.xml布局文件<?xml version=&
- 本文实例讲述了java内部类原理与用法。分享给大家供大家参考,具体如下:概念内部类:可以包含在另外一个类中的类外部类:包含内部类的类每个内部
- 在去年的时候,在各种渠道中略微的了解了SpringBoot,在开发web项目的时候是如何的方便、快捷。但是当时并没有认真的去学习下,毕竟感觉
- 我们第三章分析过客户端接入的流程, 这一小节带大家剖析客户端发送数据, Server读取数据的流程:首先温馨提示, 这一小节高度耦合第三章的
- 方法一Timer与TimerTask(Java实现)public class timerTask extends Activity{ pr
- 隐藏标题栏基于xml<application android:theme="@style/Them
- 为什么要使用Lambda?可以对一个接口进行非常简洁的实现。Lambda对接口的要求?接口中定义的抽象方法有且只有一个才可以。传统实现一个接
- SpringMVC一般使用MultipartFile来做文件的上传,通过MultipartFile的getContentType()方法判定
- 取模运算与取余运算两个概念有重叠的部分但又不完全一致。主要的区别在于对负整数进行除法运算时操作不同。对于整形数a,b来说,取模运算或者求余运
- synchronized的三种使用方式**1.修饰实例方法,**作用于当前实例加锁,进入同步代码前要获得当前实例的锁。没有问题的写法:pub
- 1.首先,八种基本数据类型分别是:int、short、float、double、long、boolean、byte、char; &
- IDEA快速搭建spring boot项目1.创建项目老规矩,点击Create New Project2.编写控制器在com.demo.sp
- 本文实例讲述了Android程序美化之自定义ListView背景的方法。分享给大家供大家参考,具体如下:在Android中,ListView
- 一,项目简介经过调查研究进行开发设计的这款仓库管理系统,主要是为商家提供商品货物进销存的信息化管理,以便让商家在竞争如此激烈的今天占据一定的
- MVP(Model-View-Presenter) 是总所周知MVC模式的一个演变,他们的主要目的都是划分模块职责,降低模块耦合,易测试,提
- 在app中经常会用到底部菜单的控件,每次都需要写好多代码,今天我们用到了前几篇博客里的控件来进一步封装底部菜单。先看效果图:主要包括以下功能
- 一、项目中配置多语言多语言的实现是通过AndroidUtilCode实现的,表示感谢!项目里面有4种语言:中文,英文,德文,俄文。文件夹如下