本文介绍了更快的是:instanceof或isInstance?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了设计问题之外,什么在现代JVM上执行得更快?

Design questions aside, what performs faster on modern JVMs?

foo instanceof Bar

Bar.class.isInstance(foo)

为什么?

推荐答案

Class.isInstance是JVM固有的.它的编译顺序与instanceof完全相同(HotSpot源代码的证明: 1 2 ).也就是说,它们在性能上是平等的.

Class.isInstance is JVM intrinsic. It is compiled to exactly the same sequence as instanceof does (the proof from HotSpot source code: 1, 2). That is, they both are equal in terms of performance.

这篇关于更快的是:instanceof或isInstance?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 16:16