class A { }

class B extends A { }

Object o1 = new A(); 
Object o2 = new B();

o1 instanceof A => true 
o1 instanceof B => false 
o2 instanceof A => true // <================ HERE 
o2 instanceof B => true

o1.getClass().equals(A.class) => true 
o1.getClass().equals(B.class) => false 
o2.getClass().equals(A.class) => false // <===============HERE 
o2.getClass().equals(B.class) => true

getClass() will be useful when you want to make sure your instance is NOT a subclass of the class you are comparing with.

java中instanceof和getClass()的区别分析的更多相关文章

  1. Java中instanceof与getClass的区别

    在比较一个类和另一个类是否属于同一个类实例的时候,通常可以采用instanceof和getClass两种方法比较两者是否相等来判断,但是两者在判断上面是有差别的,下面通过代码说明: public cl ...

  2. java中instanceof和getClass()的作用

    初学者难免有点混淆java中instanceof和getClass()的作用,  下面就来一一讲解. 父类A: class A { } 子类B: class B extends A { }  构造对象 ...

  3. Java中Comparable和Comparator接口区别分析

    Java中Comparable和Comparator接口区别分析 来源:码农网 | 时间:2015-03-16 10:25:20 | 阅读数:8902 [导读] 本文要来详细分析一下Java中Comp ...

  4. Java中instanceof和isInstance的具体区别

    Java中instanceof和isInstance的具体区别 在Think in Java泛型这一章遇到这个问题,一些博客模糊提到了isInstance是instanceof的动态实现,查阅文档参考 ...

  5. 转:Java中abstract和interface的区别

    转自:Java中abstract和interface的区别 abstract class和interface是Java语言中对于抽象类定义进行支持的两种机制,正是由于这两种机制的存在,才赋予了Java ...

  6. 转:二十一、详细解析Java中抽象类和接口的区别

    转:二十一.详细解析Java中抽象类和接口的区别 http://blog.csdn.net/liujun13579/article/details/7737670 在Java语言中, abstract ...

  7. Java中Set Map List 的区别

    java中set map list的区别: 都是集合接口 简要说明 set --其中的值不允许重复,无序的数据结构 list   --其中的值允许重复,因为其为有序的数据结构 map--成对的数据结构 ...

  8. Java中this与super的区别【6】

    若有不正之处,请多多谅解并欢迎批评指正,不甚感激.请尊重作者劳动成果: 本文原创作者:pipi-changing本文原创出处:http://www.cnblogs.com/pipi-changing/ ...

  9. Java中堆和栈的区别(转)

    栈与堆都是Java用来在Ram中存放数据的地方.与C++不同,Java自动管理栈和堆,程序员不能直接地设置栈或堆.      Java的堆是一个运行时数据区,类的对象从中分配空间.这些对象通过new. ...

随机推荐

  1. Linux环境下使用perl编写CGI(httpd)

    例子1: /var/www/cgi-bin/hello.cgi #!/usr/bin/perl print "Content-type: text/html\n\n"; print ...

  2. 【leetcode】plus One

    问题描述: Given a non-negative number represented as an array of digits, plus one to the number. The dig ...

  3. ASP.NET Identity 3.0教程

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:我相信有些人和我一样,已经开始把ASP.NET 5用于产品开发了.不过现在最大的问题是 ...

  4. VS2013缺少报表工具

    问题1:缺少报表设计工具--即rdlc无法打开设计器 原因:缺少SQL Server Data Tools(SSDT)工具 解决:安装ssdt即可 SSDT下载地址:https://msdn.micr ...

  5. VS2010 打开 VS2012 的项目

    用 VS2010 打开 VS2012 项目,只需两步. 1. 修改解决方案文件(*.sln) 使用记事本打开 *.sln 文件,将里面的 Microsoft Visual Studio Solutio ...

  6. apk 打包方式

    1 项目-->Android tools -->Export Signed  Application Package 2 在项目 manifest.xml文件下 单击“use the Ex ...

  7. Java api

    StringBuilder.charAt(int index); StringBuilder.deleteCharAt(int index); StringBuilder.setCharAt(int ...

  8. css布局2

    居中 常用居中 elemP{ text-align: center; } elelmP elemC{ display: inline-block; } elemP{ display: table; m ...

  9. rman归档删除

    rman: delete [all] input 数据库oracle 11g 全备脚本如下:rman target /  <<EOFrun {allocate channel t1 typ ...

  10. Java与.NET随笔

    .NET与Java,因这两种技术的相似性,总是会让人拿来做比较,并且总有人想让二者一分高下,最后得出孰优孰劣的结论.由于本人先用.NET,后转Java,现在.NET与Java二者并用,所以对二者间的差 ...