是否还有其他影响效率的地方
两段简短的测试代码:
Test2.java (三元运算符)
public class Test2
{
public static void main(String args[])
{
int m =1, n=2;
String s = m > n ? "a" : "b";
}
}
Test3.java ( if...else...)
public class Test3
{
public static void main(String args[])
{
int m = 1, n = 2;
if(m > n) {
String s = "a";
}
else {
String s = "b";
}
}
}
编译后用 javap 反编译得到下面指令码:
Compiled from "Test2.java"
public class Test2 extends java.lang.Object{
public Test2();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: iconst_1
1: istore_1
2: iconst_2
3: istore_2
4: iload_1 <-- 变量表索引 1 处的值压入堆栈,即 m 的值 1
5: iload_2 <-- 变量表索引 2 处的值压入堆栈,即 n 的值 2
6: if_icmple 14 <-- 比较上面两个值,若 true 继续,否则跳转到 14
9: ldc #2; //String a <-- load constant,将"a"压入堆栈
11: goto 16
14: ldc #3; //String b <-- 将"b"压入堆栈
16: astore_3 <-- 从堆栈弹出值,存储到变量表索引 3 处
17: return
}
----------------------------------------
Compiled from "Test3.java"
public class Test3 extends java.lang.Object{
public Test3();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: iconst_1
1: istore_1
2: iconst_2
3: istore_2
4: iload_1
5: iload_2
6: if_icmple 15
9: ldc #2; //String a
11: astore_3
12: goto 18
15: ldc #3; //String b
17: astore_3
18: return
}
---------------------
作者:crazygou
来源:CSDN
原文:
https://blog.csdn.net/CrazyGou/article/details/1655593 版权声明:本文为博主原创文章,转载请附上博文链接!