update test demo

This commit is contained in:
wangxiang
2023-07-26 18:53:23 +08:00
parent 213204d046
commit ecbcda6e51
7 changed files with 193 additions and 26 deletions

View File

@@ -72,6 +72,26 @@ public class TestJVM {
runMainClass(Demo7.class);
}
@Test
public void demo_while() throws Exception {
runMainClass(demo_while.class);
}
@Test
public void demo_doWhile() throws Exception {
runMainClass(demo_doWhile.class);
}
@Test
public void demo_for() throws Exception {
runMainClass(demo_for.class);
}
@Test
public void demo_foreach() throws Exception {
runMainClass(demo_foreach.class);
}
@Test
public void test_8() throws Exception {
runMainClass(Demo8.class);

View File

@@ -0,0 +1,27 @@
package haidnor.jvm.test.demo;
public class demo_doWhile {
public static void main(String[] args) {
int a = 0;
do {
a++;
} while (a < 2);
System.out.println(a);
}
}
/*
main(String[] args):
0 iconst_0
1 istore_1
2 iinc 1 by 1
5 iload_1
6 iconst_2
7 if_icmplt 2 (-5) < while (a < 2); 符合条件则向前跳转
10 getstatic #7 <java/lang/System.out : Ljava/io/PrintStream;>
13 iload_1
14 invokevirtual #13 <java/io/PrintStream.println : (I)V>
17 return
*/

View File

@@ -21,12 +21,12 @@ public class demo_exception_3 {
}
/*
main(String[] args)
0 iconst_0
1 istore_1
2 iload_1
3 invokestatic #7 <haidnor/jvm/test/demo/demo_exception.fun : (I)V>
6 goto 18 (+12)
9 astore_2
0 iconst_0
1 istore_1
2 iload_1
3 invokestatic #7 <haidnor/jvm/test/demo/demo_exception_3.fun : (I)V>
6 goto 18 (+12)
9 astore_2
10 getstatic #15 <java/lang/System.out : Ljava/io/PrintStream;>
13 ldc #21 <catch Exception>
15 invokevirtual #23 <java/io/PrintStream.println : (Ljava/lang/String;)V>
@@ -40,12 +40,11 @@ main(String[] args)
fun(int a)
0 iload_0
1 ifne 12 (+11)
4 new #29 <java/lang/ArithmeticException>
7 dup
8 invokespecial #31 <java/lang/ArithmeticException.<init> : ()V>
11 athrow
12 return
0 getstatic #15 <java/lang/System.out : Ljava/io/PrintStream;>
3 iconst_1
4 iload_0
5 idiv
6 invokevirtual #29 <java/io/PrintStream.println : (I)V>
9 return
*/

View File

@@ -21,12 +21,12 @@ public class demo_exception_4 {
}
/*
main(String[] args)
0 iconst_0
1 istore_1
2 iload_1
3 invokestatic #7 <haidnor/jvm/test/demo/demo_exception.fun : (I)V>
6 goto 18 (+12)
9 astore_2
0 iconst_0
1 istore_1
2 iload_1
3 invokestatic #7 <haidnor/jvm/test/demo/demo_exception_4.fun : (I)V>
6 goto 18 (+12)
9 astore_2
10 getstatic #15 <java/lang/System.out : Ljava/io/PrintStream;>
13 ldc #21 <catch Exception>
15 invokevirtual #23 <java/io/PrintStream.println : (Ljava/lang/String;)V>
@@ -40,12 +40,11 @@ main(String[] args)
fun(int a)
0 iload_0
1 ifne 12 (+11)
4 new #29 <java/lang/ArithmeticException>
7 dup
8 invokespecial #31 <java/lang/ArithmeticException.<init> : ()V>
11 athrow
12 return
0 getstatic #15 <java/lang/System.out : Ljava/io/PrintStream;>
3 iconst_1
4 iload_0
5 idiv
6 invokevirtual #29 <java/io/PrintStream.println : (I)V>
9 return
*/

View File

@@ -0,0 +1,31 @@
package haidnor.jvm.test.demo;
public class demo_for {
public static void main(String[] args) {
int a = 0;
for (int i = 0; i < 3; i++) {
a++;
}
System.out.println(a);
}
}
/*
main(String[] args):
0 iconst_0 < int a = 0
1 istore_1
2 iconst_0 < int i =0
3 istore_2
4 iload_2
5 iconst_3 < 比较值3
6 if_icmpge 18 (+12) < (i < 3)
9 iinc 1 by 1 < a++
12 iinc 2 by 1 < i++
15 goto 4 (-11) < 向前跳转代码
18 getstatic #7 <java/lang/System.out : Ljava/io/PrintStream;>
21 iload_1
22 invokevirtual #13 <java/io/PrintStream.println : (I)V>
25 return
*/

View File

@@ -0,0 +1,62 @@
package haidnor.jvm.test.demo;
public class demo_foreach {
public static void main(String[] args) {
int[] arr = new int[]{999, 333};
for (int i : arr) {
System.out.println(i);
}
}
}
/*
main(String[] args):
0 iconst_2 < 操作数栈压入2,这个是 arr[] 的长度
1 newarray 10 (int) < 创建数组
3 dup
4 iconst_0
5 sipush 999
8 iastore < arr[0] = 999
9 dup < 复制栈顶的 arr[], 压入操作数栈
10 iconst_1
11 sipush 333
14 iastore < arr[1] = 333
15 astore_1
16 aload_1
17 astore_2
18 aload_2
19 arraylength
20 istore_3
21 iconst_0
22 istore 4
24 iload 4
26 iload_3
27 if_icmpge 50 (+23)
30 aload_2
31 iload 4
33 iaload
34 istore 5
36 getstatic #7 <java/lang/System.out : Ljava/io/PrintStream;>
39 iload 5
41 invokevirtual #13 <java/io/PrintStream.println : (I)V>
44 iinc 4 by 1
47 goto 24 (-23)
50 return
+--------+-------+--------+--------+-----------+-------------------------+
| Nr. | 起始PC | 长度 | 序号 | 名字 | 描述符(存储类型) |
+--------+-------+--------+--------+-----------+-------------------------+
| 0 | 0 | 51 | 0 | args | [Ljava/lang/String; |
+--------+-------+--------+--------+-----------+-------------------------+
| 1 | 16 | 35 | 1 | arr | [I |
+--------+-------+--------+--------+-----------+-------------------------+
| 2 | 36 | 8 | 5 | i | I |
+--------+-------+--------+--------+-----------+-------------------------+
*/

View File

@@ -0,0 +1,29 @@
package haidnor.jvm.test.demo;
public class demo_while {
public static void main(String[] args) {
int a = 0;
while (a < 2) {
a++;
}
System.out.println(a);
}
}
/*
main(String[] args):
0 iconst_0
1 istore_1
2 iload_1
3 iconst_2
4 if_icmpge 13 (+9) < 符合条件就进入循环体
7 iinc 1 by 1 < a++
10 goto 2 (-8) < 向前跳转指令
13 getstatic #7 <java/lang/System.out : Ljava/io/PrintStream;>
16 iload_1
17 invokevirtual #13 <java/io/PrintStream.println : (I)V>
20 return
*/