1 Test.java
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
int a = 0;
int b = 1;
int c = 10;
int d = a + b;
}
}
2 运行:
E:\jvm>javac Test.java
E:\jvm>javap -c Test
Compiled from “Test.java”
public class Test extends java.lang.Object{
public Test();
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_0
1: istore_1
2: iconst_1
3: istore_2
4: bipush 10
6: istore_3
7: iload_1
8: iload_2
9: iadd
10: istore 4
12: return
}
If else测试:
public class Test {
public static void main(String[] args) {
int a = 0;
int b = 1;
int c = 2;
if( a > b ) {
c++;
} else if ( a < c ) {
c–;
} else {
c = c + 10;
}
System.out.println(c);
}
}
运行测试:
E:\jvm>javac Test.java
E:\jvm>java Test
1
E:\jvm>javap -c Test
Compiled from “Test.java”
public class Test extends java.lang.Object{
public Test();
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_0
1: istore_1
2: iconst_1
3: istore_2
4: iconst_2
5: istore_3
6: iload_1
7: iload_2
8: if_icmple 17
11: iinc 3, 1
14: goto 33
17: iload_1
18: iload_3
19: if_icmpge 28
22: iinc 3, -1
25: goto 33
28: iload_3
29: bipush 10
31: iadd
32: istore_3
33: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
36: iload_3
37: invokevirtual #3; //Method java/io/PrintStream.println:(I)V
40: return
}
Do while测试:
public class Test {
public static void main(String[] args) {
int a = 0;
int result = 0;
do {
result += a;
a++;
} while(a<100);
System.out.println(result);
}
}
运行测试:
E:\jvm>javac Test.java
E:\jvm>java Test
4950
E:\jvm>javap -c Test
Compiled from “Test.java”
public class Test extends java.lang.Object{
public Test();
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_0
1: istore_1
2: iconst_0
3: istore_2
4: iload_2
5: iload_1
6: iadd
7: istore_2
8: iinc 1, 1
11: iload_1
12: bipush 100
14: if_icmplt 4
17: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
20: iload_2
21: invokevirtual #3; //Method java/io/PrintStream.println:(I)V
24: return
}
While do测试:
public class Test {
public static void main(String[] args) {
int a = 0;
int result = 0;
while (a < 100) {
result += a;
a++;
}
System.out.println(result);
}
}
运行测试:
E:\jvm>javap -c Test
Compiled from “Test.java”
public class Test extends java.lang.Object{
public Test();
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_0
1: istore_1
2: iconst_0
3: istore_2
4: iload_1
5: bipush 100
7: if_icmpge 20
10: iload_2
11: iload_1
12: iadd
13: istore_2
14: iinc 1, 1
17: goto 4
20: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
23: iload_2
24: invokevirtual #3; //Method java/io/PrintStream.println:(I)V
27: return
}
循环体测试:
1 Test.java
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
double i;
for (i = 10; i < 100.0; i++) {
;
}
}
}
2 运行:
E:\jvm>javac Test.java
E:\jvm>javap -c Test
Compiled from “Test.java”
public class Test extends java.lang.Object{
public Test();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object.”<init>”:()V
4: return
public static void main(java.lang.String[]);
Code:
0: ldc2_w #2; //double 10.0d
3: dstore_1
4: dload_1
5: ldc2_w #4; //double 100.0d
8: dcmpg
9: ifge 19
12: dload_1
13: dconst_1
14: dadd
15: dstore_1
16: goto 4
19: return
}
Switch语句测试:
public class Test {
public static void main(String[] args) {
int a = 0;
int c = 10;
switch (a) {
case 0 : c++;
break;
case 1 : c–;
break;
default: c++;
}
System.out.println(c);
}
}
运行测试:
E:\jvm>javac Test.java
E:\jvm>java Test
11
E:\jvm>javap -c Test
Compiled from “Test.java”
public class Test extends java.lang.Object{
public Test();
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_0
1: istore_1
2: bipush 10
4: istore_2
5: iload_1
6: lookupswitch{ //2
0: 32;
1: 38;
default: 44 }
32: iinc 2, 1
35: goto 47
38: iinc 2, -1
41: goto 47
44: iinc 2, 1
47: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
50: iload_2
51: invokevirtual #3; //Method java/io/PrintStream.println:(I)V
54: return
}
参数传递:
public class Test {
public static void main(String[] args) {
int c = add(1,2);
System.out.println(c);
}
private static int add(int a, int b) {
return a + b;
}
}
运行测试:
E:\jvm>javap -c Test
Compiled from “Test.java”
public class Test extends java.lang.Object{
public Test();
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: iconst_2
2: invokestatic #2; //Method add:(II)I
5: istore_1
6: getstatic #3; //Field java/lang/System.out:Ljava/io/PrintStream;
9: iload_1
10: invokevirtual #4; //Method java/io/PrintStream.println:(I)V
13: return
}
public class Test {
public static void main(String[] args) {
int a = 1;
int b = 10;
int c = 0;
try {
int i = 1 / c;
} catch(Exception e)
{
//
}
finally{
}
}
}
E:\jvm>javac Test.java
E:\jvm>java Test
E:\jvm>javap -c Test
Compiled from “Test.java”
public class Test extends java.lang.Object{
public Test();
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: bipush 10
4: istore_2
5: iconst_0
6: istore_3
7: iconst_1
8: iload_3
9: idiv
10: istore 4
12: goto 25
15: astore 4
17: goto 25
20: astore 5
22: aload 5
24: athrow
25: return
Exception table:
from to target type
7 12 15 Class java/lang/Exception
7 12 20 any
15 17 20 any
20 22 20 any
}
synchronized(args)
3: monitorenter
4: aload_1
5: monitorexit
Exception table:
from to target type
4 6 9 any
9 12 9 any
Field 测试:
Test.java
public class Test {
public static String username = “username”;
private static int age = 0;
public static void main(String[] args) {
String name = username;
System.out.println(name);
}
}
E:\jvm>javap -c Test
Compiled from “Test.java”
public class Test extends java.lang.Object{
public static java.lang.String username;
public Test();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object.”<init>”:()V
4: return
public static void main(java.lang.String[]);
Code:
0: getstatic #2; //Field username:Ljava/lang/String;
3: astore_1
4: getstatic #3; //Field java/lang/System.out:Ljava/io/PrintStream;
7: aload_1
8: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/Str
ing;)V
11: return
static {};
Code:
0: ldc #5; //String username
2: putstatic #2; //Field username:Ljava/lang/String;
5: iconst_0
6: putstatic #6; //Field age:I
9: return
}
Person类及编译结果:
public class Person …{
String name;
int age;
public void setAge(int age)
…{
this.age = age;
}
public void setName(String name)
…{
this.name = name;
}
private int getAge()
…{
return age;
}
private String getName()
…{
return name;
}
}
E:jvm>javap -c Person
Compiled from “Person.java”
public class Person extends java.lang.Object…{
java.lang.String name;
int age;
public Person();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object.”<init>”:()V
4: return
public void setAge(int);
Code:
0: aload_0
1: iload_1
2: putfield #2; //Field age:I
5: return
public void setName(java.lang.String);
Code:
0: aload_0
1: aload_1
2: putfield #3; //Field name:Ljava/lang/String;
5: return
}
Inner Class:
public class Person …{
String name;
int age;
public void setAge(int age)
…{
this.age = age;
}
public void setName(String name)
…{
this.name = name;
}
int getAge()
…{
return age;
}
private String getName()
…{
return name;
}
}
class ManPerson extends Person …{
int getAge()…{
return super.getAge();
}
}
E:jvm>javap -c Person
Compiled from “Person.java”
public class Person extends java.lang.Object…{
java.lang.String name;
int age;
public Person();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object.”<init>”:()V
4: return
public void setAge(int);
Code:
0: aload_0
1: iload_1
2: putfield #2; //Field age:I
5: return
public void setName(java.lang.String);
Code:
0: aload_0
1: aload_1
2: putfield #3; //Field name:Ljava/lang/String;
5: return
int getAge();
Code:
0: aload_0
1: getfield #2; //Field age:I
4: ireturn
}
E:jvm>
类的调用实例:
public class Person …{
String name;
int age;
public void setAge(int age)
…{
this.age = age;
}
public void setName(String name)
…{
this.name = name;
}
int getAge()
…{
return age;
}
private String getName()
…{
return name;
}
}
class ManPerson extends Person …{
int getAge()…{
return super.getAge();
}
}
public class Test…{
public static void main(String[] args)…{
System.out.println(“test”);
Person p = new Person();
p.setName(“psetName”);
p.setAge(22);
System.out.println(p.getAge());
}
}
翻译结果:
E:jvm>javap -c Test
Compiled from “Test.java”
public class Test extends java.lang.Object…{
public Test();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object.”<init>”:()V
4: return
public static void main(java.lang.String[]);
Code:
0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3; //String test
5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/Str
ing;)V
8: new #5; //class Person
11: dup
12: invokespecial #6; //Method Person.”<init>”:()V
15: astore_1
16: aload_1
17: ldc #7; //String psetName
19: invokevirtual #8; //Method Person.setName:(Ljava/lang/String;)V
22: aload_1
23: bipush 22
25: invokevirtual #9; //Method Person.setAge:(I)V
28: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
31: aload_1
32: invokevirtual #10; //Method Person.getAge:()I
35: invokevirtual #11; //Method java/io/PrintStream.println:(I)V
38: return
}