instanceof

说明:

本系列博客是本人在工作中遇到的一些问题的整理,其中有些资料来源网络博客,有些信息来自出版的书籍,掺杂一些个人的猜想及验证,总结,主要目的是方便知识的查看,并非纯原创。本系列博客会不断更新。原创不容易,支持原创。对于参考的一些其他博客,会尽量把博客地址列在博客的后面,以方便知识的查看。

 

instanceof是Java的一个二元操作符,用于判断关键字左边的对象是否是它右边的类型的实例,返回值为boolean类型。

 

看下如下情形,A为接口,B实现接口A,C继承类B,D没有显式继承任何类,也没有实现接口:

/**

 * Created with IntelliJ IDEA.

 * User: yejunwu123@gmail.com

 * Date: 2014-08-17 20:24

 * Description:接口

 */

public interface A {

}

 

/**

 * Created with IntelliJ IDEA.

 * User: yejunwu123@gmail.com

 * Date: 2014-08-17 20:25

 * Description:父类,实现接口A

 */

public class B implements A{

}

 

/**

 * Created with IntelliJ IDEA.

 * User: yejunwu123@gmail.com

 * Date: 2014-08-17 20:25

 * Description:子类,继承B

 */

public class C extends B{

}

 

/**

 * Created with IntelliJ IDEA.

 * User: yejunwu123@gmail.com

 * Date: 2014-08-19 11:04

 * Description:

 */

public class D {

}

 

看下测试类:
图中有红线的是编译不通过的,注释掉

/**

 * Created with IntelliJ IDEA.

 * User: yejunwu123@gmail.com

 * Date: 2014-08-17 20:26

 * Description:

 * 测试java中的instanceof关键字

 * A为接口

 * 类B实现接口A

 * 类C继承类B

 */

public class InstanceofTest {

    public static void main(String[] args) {

        //创建对象

        A ab = new B();

        A ac = new C();

 

        B bb = new B();

        B bc = new C();

 

        C cc = new C();

 

        D dd = new D();

 

        //..instanceof A

        System.out.println("ab instanceof A = " + (ab instanceof A));

        System.out.println("ac instanceof A = " + (ac instanceof A));

        System.out.println("bb instanceof A = " + (bb instanceof A));

        System.out.println("bc instanceof A = " + (bc instanceof A));

        System.out.println("cc instanceof A = " + (cc instanceof A));

        System.out.println("dd instanceof A = " + (dd instanceof A));

        System.out.println("null instanceof A = " + (null instanceof A));

        System.out.println("-------------------------------------");

        //..instanceof B

        System.out.println("ab instanceof B = " + (ab instanceof B));

        System.out.println("ac instanceof B = " + (ac instanceof B));

        System.out.println("bb instanceof B = " + (bb instanceof B));

        System.out.println("bc instanceof B = " + (bc instanceof B));

        System.out.println("cc instanceof B = " + (cc instanceof B));

        //System.out.println("dd instanceof B = " + (dd instanceof B));

        System.out.println("null instanceof B = " + (null instanceof B));

        System.out.println("-------------------------------------");

        //..instanceof C

        System.out.println("ab instanceof C = " + (ab instanceof C));

        System.out.println("ac instanceof C = " + (ac instanceof C));

        System.out.println("bb instanceof C = " + (bb instanceof C));

        System.out.println("bc instanceof C = " + (bc instanceof C));

        System.out.println("cc instanceof C = " + (cc instanceof C));

        //System.out.println("dd instanceof C = " + (dd instanceof C));

        System.out.println("null instanceof C = " + (null instanceof C));

        System.out.println("-------------------------------------");

        //..instanceof D

        System.out.println("ab instanceof D = " + (ab instanceof D));

        System.out.println("ac instanceof D = " + (ac instanceof D));

        //System.out.println("bb instanceof D = " + (bb instanceof D));

        //System.out.println("bc instanceof D = " + (bc instanceof D));

        //System.out.println("cc instanceof D = " + (cc instanceof D));

        System.out.println("dd instanceof D = " + (dd instanceof D));

        System.out.println("null instanceof D = " + (null instanceof D));

    }

}

 

看下测试结果:

ab instanceof A = true

ac instanceof A = true

bb instanceof A = true

bc instanceof A = true

cc instanceof A = true

dd instanceof A = false

null instanceof A = false

-------------------------------------

ab instanceof B = true

ac instanceof B = true

bb instanceof B = true

bc instanceof B = true

cc instanceof B = true

null instanceof B = false

-------------------------------------

ab instanceof C = false

ac instanceof C = true

bb instanceof C = false

bc instanceof C = true

cc instanceof C = true

null instanceof C = false

-------------------------------------

ab instanceof D = false

ac instanceof D = false

dd instanceof D = true

null instanceof D = false

 

不难看出对象的实际类型(注意是实际类型,new关键字后的类型,而不是引用的类型)instanceof关键字后的类型的关系:

1、对象 instanceof 本身类型 = true;ab instanceof B = true,bb instanceof B = true,ac instanceof C = true,bc instanceof C = true,cc instanceof C = true

2、对象 instanceof 本身类型的父类 = true;ac instanceof B = true,bc instanceof B = true,

cc instanceof B = true

3、对象 instanceof 本身类型实现的接口 = true;ab instanceof A = true,ac instanceof A = true,

bb instanceof A = true,bc instanceof A = true,cc instanceof A = true

4null instanceof 任何类型 = false

 

使用instanceof时在编译期间,就会判断操作符左边的对象是否在右边的类型继承层次中,如果不在同一个继承层次中编译就会报错。

 

但是这边有个疑问,dd instanceof A通过了编译,结果为false,具体细节不太清楚,为什么instanceof后的类型为接口能通过编译。

 

有意思的是,知乎上有个关于instanceof的讨论,有兴趣的可以看下,本人稍微看了下,反正是边看边笑,那感觉就像是学渣看学霸在讲题,呵呵呵呵

http://www.zhihu.com/question/21574535

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值