java 零散知识汇总(初级)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 零散知识汇总(初级)相关的知识,希望对你有一定的参考价值。

instanceof 运算符用法:

  instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例。

技术分享
public class Temp2 {
    interface A {
    }

    static class B implements A {

    }

    static class C extends B {

    }

    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

*/
}
View Code

二、

 

以上是关于java 零散知识汇总(初级)的主要内容,如果未能解决你的问题,请参考以下文章

C#初级阶段-学习收获及错题(知识点)汇总

Java阿里等大厂面试题汇总,值得收藏!

产品经理初级到高级书单汇总

Java零散知识点

Java零散知识点

什么是ZK-Rollup(零知识汇总)?