Java里面instanceof怎么实现的
开始完全一头雾水呀,后面看了Java指令集的介绍,逐渐理解了。
https://www.zhihu.com/question/21574535/answer/18998914
下面这个答案比较直白
你在面月薪10000的Java高级研发职位。面试官对JVM有一些了解,想让你说说JVM会如何实现 instanceof 指令。
但他可能也没看过实际的JVM是怎么做的,只是臆想过一下而已。JVM的规定就是“底层”。这种情况就给他JVM规范对 instanceof 指令的定义就好:
Chapter 6. The Java Virtual Machine Instruction Set, JVM规范Java SE 7版
根据规范来臆想一下实现就能八九不离十的混过这题了。 地址:
http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-6.html#jvms-6.5.instanceof
上面指令的定义详细解释如下(解释和重要点已经飘红指出):
instanceof
Operation
Determine if object is of given type
Format
instanceof
indexbyte1
indexbyte2
Forms
instanceof = 193 (0xc1)
Operand Stack
..., objectref →
..., result
Description
The objectref, which must be of type reference, is popped from the operand stack. The unsigned indexbyte1 and indexbyte2 are used to construct an index into the run-time constant pool of the current class (§2.6), where the value of the index is (indexbyte1 << 8) | indexbyte2. The run-time constant pool item at the index must be a symbolic reference to a class, array, or interface type.
If objectref is null, the instanceof instruction pushes an int result of 0 as an int on the operand stack.
意思是左侧是null,就返回false.(另外,右侧不能为null)
Otherwise, the named class, array, or interface type is resolved (§5.4.3.1). If objectref is an instance of the resolved class or array or implements the resolved interface, the instanceof instruction pushes an int result of 1 as an int on the operand stack; otherwise, it pushes an int result of 0.
这个只是概述,下面是详细解释。分为多钟情况。
The following rules are used to determine whether an objectref that is not null is an instance of the resolved type: If S is the class of the object referred to by objectref and T is the resolved class, array, or interface type, instanceof determines whether objectref is an instance of T as follows:
If S is an ordinary (nonarray) class, then:
If T is a class type, then S must be the same class as T, or S must be a subclass of T;
If T is an interface type, then S must implement interface T.
If S is an interface type, then:
If T is a class type, then T must be
Object.If T is an interface type, then T must be the same interface as S or a superinterface of S.
If S is a class representing the array type SC
[], that is, an array of components of type SC, then:If T is a class type, then T must be
Object.If T is an interface type, then T must be one of the interfaces implemented by arrays (JLS §4.10.3).
If T is an array type TC
[], that is, an array of components of type TC, then one of the following must be true:TC and SC are the same primitive type.
TC and SC are reference types, and type SC can be cast to TC by these run-time rules.
Linking Exceptions
During resolution of the symbolic reference to the class, array, or interface type, any of the exceptions documented in §5.4.3.1 can be thrown.
Notes
The instanceof instruction is very similar to the checkcast instruction (§checkcast). It differs in its treatment of null(上面提到,左侧null会直接返回false), its behavior when its test fails (checkcast throws an exception, instanceof pushes a result code), and its effect on the operand stack.
下面这个答案比较“晦涩”
你在面试月薪10000以上的Java资深研发职位,注重性能调优啥的。这种职位虽然不直接涉及JVM的研发,但由于性能问题经常源自“抽象泄漏”,
对实际使用的JVM的实现的思路需要有所了解。面试官对JVM的了解可能也就在此程度。
对付这个可以用一篇论文:Fast subtype checking in the HotSpot JVM。
之前有个讨论帖里讨论过对这篇论文的解读:请教一个share/vm/oops下的代码做fast subtype check的问题 其中提到了 “抽象泄露”,后面会单开文章分析。
再看下面的代码例子(http://jurisp.iteye.com/blog/815704)
String s = null;
System.out.println(s instanceof Object); // false System.out.println(s instanceof String); // false // 下面的不能通过编译 Incompatible conditional operand types String and String[]
// boolean as = (s instanceof String[]); // 不能编译 Syntax error on token "null", invalid ReferenceType
// System.out.println(s instanceof null); // 不能编译 Incompatible conditional operand types TypeInstanceTest and String//System.out.println(new Solution() instanceof String); System.out.println(new String() instanceof String); //true System.out.println(new String() instanceof Object); //true // 会抛出异常
Solution test = (Solution) new Object();
而运行结果如下:
false
false
true
true
Exception
说明不是随便两个类型之间都能用instanceof的,如果两个操作数的类型都是类,其中一个必须是另一个的子类型。
另外,注意下面这段晦涩的表述。
访问静态方法,使用表达式作为限定符。表达式的值所引用的对象的运行期类型在确定哪一个方法被调用的时候不起作用,而且对象有标识符,其标识符也不起任何作用。
代码示例如下:
class Solution {
public static boolean canFinish() {
System.out.println("here");
return true;
}
public static void main(String[] args) {
((Solution) null).canFinish();
}
}
运行得到正确调用结果:
here
说明,用null是可以调用 static 方法的。
Java里面instanceof怎么实现的的更多相关文章
- java中instanceof和getClass()的作用
初学者难免有点混淆java中instanceof和getClass()的作用, 下面就来一一讲解. 父类A: class A { } 子类B: class B extends A { } 构造对象 ...
- 深入Java关键字instanceof
深入Java关键字instanceof instanceof关键字用于判断一个引用类型变量所指向的对象是否是一个类(或接口.抽象类.父类)的实例. 举个例子: public interface ...
- Java-Runoob-高级教程-实例-方法:07. Java 实例 – instanceOf 关键字用法
ylbtech-Java-Runoob-高级教程-实例-方法:07. Java 实例 – instanceOf 关键字用法 1.返回顶部 1. Java 实例 - instanceof 关键字用法 ...
- Java关键字instanceof
深入Java关键字instanceof instanceof关键字用于判断一个引用类型变量所指向的对象是否是一个类(或接口.抽象类.父类)的实例. 举个例子: public interfa ...
- Java中instanceof和isInstance的具体区别
Java中instanceof和isInstance的具体区别 在Think in Java泛型这一章遇到这个问题,一些博客模糊提到了isInstance是instanceof的动态实现,查阅文档参考 ...
- Java 实例 - instanceof 关键字用法
Java 实例 - instanceof 关键字用法 instanceof 是 Java 的一个二元操作符,类似于 ==,>,< 等操作符. instanceof 是 Java 的保留关键 ...
- Java关键字——instanceof
Java中可以使用instanceof关键字判断一个对象到底是哪一个类的实例 格式:对象 instance 类 返回 boolean类型 通过子类实例化的对象同时是子类和父类的实例,无论是直接声明子类 ...
- Java中instanceof用法
java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例.instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例. 用法:resu ...
- JAVA的instanceOf什么时候用啊
当你拿到一个对象的引用时(例如参数),你可能需要判断这个引用真正指向的类.所以你需要从该类继承树的最底层开始, 使用instanceof操作符判断,第一个结果为true的类即为引用真正指向的类. cl ...
随机推荐
- MVC 强类型视图
MVC里面的强类型视图的确是一个很强大的东西,结合EF(Entity Framework)的话更加强大,可以直接把你的数据库直接生成强视图的增删查改的视图,在视图中所有Model的属性均动态的,我们不 ...
- CSS绝对定位和相对定位 position: absolute/relative
absolute(绝对定位): 会把对象拖离HTML文档流,并通过top, left, right, bottom确定对象的具体位置,这个四个位置属性至少要设置一个,否则无法激活对象的absolute ...
- Sqli-labs less 28
Less-28 本关考察内容与27关没有太大的差距,我们直接给出一个payload: http://127.0.0.1/sqllib/Less-28/?id=100')union%a0select(1 ...
- VMware 11 设置U盘启动,总是找不到physicalDrive1
问题:VMware 11 设置U盘启动,总是找不到physicalDrive1 先说一下U盘启动的设置:如图 解决方案: 有两个服务没有进行启动,VMware USB Arbitration Serv ...
- ResourceBundle使用
一.认识国际化资源文件 这个类提供软件国际化的捷径.通过此类,可以使您所编写的程序可以: 轻松地本地化或翻译成不同的语言 一次处理多个语言环境 ...
- (4)用opengl读入off文件生成可执行文件把模型显示出来(未完待续)
·找了好几个程序,好像都达不到我的要求,去教程里看看吧! 在往上抛出了这问题,好几天才有人回答,我已经找到程序了 正好的他的分析对我分解程序很有用 这是一个难度比较高的 首先你要分析.off文件结构, ...
- 知问前端——对话框UI(一)
对话框(dialog),是jQuery UI非常重要的一个功能.它彻底的代替了JavaScript的alert().prompt()等方法,也避免了新窗口或页面的繁杂冗余. 开启多个dialog 我们 ...
- Debian - 设置MYSQL开机启动
设置MYSQL 首先拷贝mysql.server到/etc/init.d目录下命名为mysql # cp /自己的安装目录/mysql/share/mysql/mysql.server /etc/in ...
- (转载) .NET2.0程序集无法在.net 4.0 中运行的解决方案
首先在MSDN上看到 4.0 的更新日志中有如下这条: .NET Framework 4 不能自动使用自己的公共语言运行时版本来运行由 .NET Framework 早期版本生成的应用程序. 若要使用 ...
- lintcode:买卖股票的最佳时机 III
买卖股票的最佳时机 III 假设你有一个数组,它的第i个元素是一支给定的股票在第i天的价格.设计一个算法来找到最大的利润.你最多可以完成两笔交易. 样例 给出一个样例数组 [4,4,6,1,1,4,2 ...