Java 字节码 - 为什么要跳过偏移量?
Java bytecode - Why is offset being skipped?
我有这个很简单class
public class TestImpl2 {
public TestImpl2() {
}
public double run(double param) {
double d = 7.0D;
double k = 4.0D;
if (param < k) {
System.out.println(d);
}
return 0.0D;
}
}
我用 javac 编译然后用 javap 反编译以查看其字节码。
0: ldc2_w #14 // double 7.0d
3: dstore_3
4: ldc2_w #16 // double 4.0d
7: dstore 5
9: dload_1
10: dload 5
12: dcmpg
13: ifge 23
16: getstatic #23 // Field java/lang/System.out:Ljava/io/PrintStream;
19: dload_3
20: invokevirtual #29 // Method java/io/PrintStream.println:(D)V
23: dconst_0
24: dreturn
让我们检查偏移量
0 - 保留给“这个”参考
1 - 是方法参数
2 - 跳过了?
3 - 变量“d”
4 - 跳过了?
5 - 变量“k”
为什么跳过偏移量 2 和 4?是因为方法参数、d 和 k 是双精度数还是完全不同?
是的,这是因为它们是双重的。在 Java Virtual Machine Specification 部分 2.6.1 局部变量中,您可以阅读:
单个局部变量可以保存类型为 boolean、byte、char、short、int、float、reference 或 returnAddress 的值。一对局部变量可以保存一个 long 或 double 类型的值。
根据 JVM spec(强调我的):
Local variables are addressed by indexing. The index of the first
local variable is zero. An integer is considered to be an index into
the local variable array if and only if that integer is between zero
and one less than the size of the local variable array.
A value of type long
or type double
occupies two consecutive local
variables. Such a value may only be addressed using the lesser index.
For example, a value of type double stored in the local variable array
at index n actually occupies the local variables with indices n and
n+1; however, the local variable at index n+1 cannot be loaded from.
不是没有用到索引2和4。只是param
和d
是double
,各占2个空格
我有这个很简单class
public class TestImpl2 {
public TestImpl2() {
}
public double run(double param) {
double d = 7.0D;
double k = 4.0D;
if (param < k) {
System.out.println(d);
}
return 0.0D;
}
}
我用 javac 编译然后用 javap 反编译以查看其字节码。
0: ldc2_w #14 // double 7.0d
3: dstore_3
4: ldc2_w #16 // double 4.0d
7: dstore 5
9: dload_1
10: dload 5
12: dcmpg
13: ifge 23
16: getstatic #23 // Field java/lang/System.out:Ljava/io/PrintStream;
19: dload_3
20: invokevirtual #29 // Method java/io/PrintStream.println:(D)V
23: dconst_0
24: dreturn
让我们检查偏移量
0 - 保留给“这个”参考
1 - 是方法参数
2 - 跳过了?
3 - 变量“d”
4 - 跳过了?
5 - 变量“k”
为什么跳过偏移量 2 和 4?是因为方法参数、d 和 k 是双精度数还是完全不同?
是的,这是因为它们是双重的。在 Java Virtual Machine Specification 部分 2.6.1 局部变量中,您可以阅读:
单个局部变量可以保存类型为 boolean、byte、char、short、int、float、reference 或 returnAddress 的值。一对局部变量可以保存一个 long 或 double 类型的值。
根据 JVM spec(强调我的):
Local variables are addressed by indexing. The index of the first local variable is zero. An integer is considered to be an index into the local variable array if and only if that integer is between zero and one less than the size of the local variable array.
A value of type
long
or typedouble
occupies two consecutive local variables. Such a value may only be addressed using the lesser index. For example, a value of type double stored in the local variable array at index n actually occupies the local variables with indices n and n+1; however, the local variable at index n+1 cannot be loaded from.
不是没有用到索引2和4。只是param
和d
是double
,各占2个空格