JVM method translate to bytecode:修订间差异

来自WHY42
(Riguz移动页面JVM:Java 代码与bytecode对应JVM method translate to bytecode,不留重定向)
 
(未显示同一用户的3个中间版本)
第35行: 第35行:
dup
dup
invokespecial #6                  // Method "<init>":()V
invokespecial #6                  // Method "<init>":()V
</syntaxhighlight>
== 变量赋值 ==
<syntaxhighlight lang="java">
HelloWorld s = new HelloWorld();
</syntaxhighlight>
<syntaxhighlight lang="lisp" highlight="4">
new          #5                  // class HelloWorld
dup
invokespecial #6                  // Method "<init>":()V
astore_1
</syntaxhighlight>
值得注意的是,因为局部变量表中是按照slot来的,所以存储long和double的时候会占用两个slot,这样store的slot序号跟局部变量的顺序并不是完全对应的。
== loop ==
<syntaxhighlight lang="java">
void spin() {
    int i;
    for (i = 0; i < 100; i++) {
        ;    // Loop body is empty
    }
}
</syntaxhighlight>
<syntaxhighlight lang="lisp" highlight="4">
0  iconst_0      // Push int constant 0
1  istore_1      // Store into local variable 1 (i=0)
2  goto 8        // First time through don't increment
5  iinc 1 1      // Increment local variable 1 by 1 (i++)
8  iload_1        // Push local variable 1 (i)
9  bipush 100    // Push int constant 100
11  if_icmplt 5    // Compare and loop if less than (i < 100)
14  return        // Return void when done
</syntaxhighlight>
</syntaxhighlight>


[[Category:JVM]]
[[Category:JVM]]

2023年12月19日 (二) 06:59的最新版本

特殊方法

xxx

默认构造函数

aload_0
invokespecial #1                  // Method java/lang/Object."<init>":()V
return

其中,

  • 构造函数会通过invokevirtual被调用,这里aload_0可以取到this
  • 构造函数中会去调用父类的构造函数

statements

创建对象

HelloWorld s = new HelloWorld();
new           #5                  // class HelloWorld
dup
invokespecial #6                  // Method "<init>":()V

变量赋值

HelloWorld s = new HelloWorld();
new           #5                  // class HelloWorld
dup
invokespecial #6                  // Method "<init>":()V
astore_1

值得注意的是,因为局部变量表中是按照slot来的,所以存储long和double的时候会占用两个slot,这样store的slot序号跟局部变量的顺序并不是完全对应的。

loop

void spin() {
    int i;
    for (i = 0; i < 100; i++) {
        ;    // Loop body is empty
    }
}
0   iconst_0       // Push int constant 0
1   istore_1       // Store into local variable 1 (i=0)
2   goto 8         // First time through don't increment
5   iinc 1 1       // Increment local variable 1 by 1 (i++)
8   iload_1        // Push local variable 1 (i)
9   bipush 100     // Push int constant 100
11  if_icmplt 5    // Compare and loop if less than (i < 100)
14  return         // Return void when done