Java的交换机是如何工作的? [英] How does Java's switch work under the hood?
本文介绍了Java的交换机是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Java的switch语句如何在幕后工作?它如何比较正在使用的变量的值与案例部分中给出的值?它是否使用 ==
或 .equals()
,还是完全不同的其他内容?
How does Java's switch statement work under the hood? How does it compare the value of the variable being used, to those given in the case parts? Does it use ==
or .equals()
, or is it something else entirely?
我主要对前1.7版本感兴趣。
I'm mainly interested in the pre 1.7 versions.
推荐答案
两者都没有。 它使用 lookupswitch
JVM指令,本质上是一个表查找。看一下以下示例的字节码:
Neither. it uses the lookupswitch
JVM instruction, which is essentially a table lookup. Take a look at the bytecode of the following example:
public static void main(String... args) {
switch (1) {
case 1:
break;
case 2:
break;
}
}
public static void main(java.lang.String[]);
Code:
Stack=1, Locals=1, Args_size=1
0: iconst_1
1: lookupswitch{ //2
1: 28;
2: 31;
default: 31 }
28: goto 31
31: return
这篇关于Java的交换机是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文