public void dup() { Object o = new Object(); } // 对应的字节码 public void dup(); descriptor: ()V flags: ACC_PUBLIC Code: stack=2, locals=2, args_size=1 0: new // class java/lang/Object 3: dup 4: invokespecial // Method java/lang/Object."<init>":()V 7: astore_1 8: return
public static boolean judge() { return false; } public void pop() { judge(); } // 对应的字节码 public void pop(); descriptor: ()V flags: ACC_PUBLIC Code: stack=1, locals=1, args_size=1 0: invokestatic // Method judge:()Z 3: pop 4: return
类型 | 常数指令 | 范围 |
---|---|---|
int/short/char/byte/boolean | iconst | [-1,5] |
bipush | [-128,127] | |
sipush | [-32768,32767] | |
ldc | any int value | |
long | lconst | 0,1 |
ldc | any long value | |
float | fconst | 0,1,2 |
ldc | any float value | |
double | dconst | 0,1 |
ldc | any double value | |
reference | aconst | null |
ldc | String literal,Class literal |
public void locals(long l, float f) { { int i = 0; } { String s = "Hello Word"; } } // 对应的字节码 public void locals(long, float); descriptor: (JF)V flags: ACC_PUBLIC Code: stack=1, locals=5, args_size=3 0: iconst_0 1: istore 4 3: ldc // String Hello Word 5: astore 4 7: return
istore 4
和 astore 4
stack=1, locals=5, args_size=3
类型 | 加载指令 | 存储指令 |
---|---|---|
int/short/char/byte/boolean | iload | istore |
long | lload | lstore |
float | fload | fstore |
double | dload | dstore |
reference | aload | astore |
public void innc() { for (int i = 0; i < 100; i++) { } } // 对应的字节码 public void innc(); descriptor: ()V flags: ACC_PUBLIC Code: stack=2, locals=2, args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 100 5: if_icmpge 14 8: iinc 1, 1 // i++ 11: goto 2 14: return
public class PutField { private Object obj; public void putFiled() { obj = new Object(); } } // 对应的字节码 public void putFiled(); descriptor: ()V flags: ACC_PUBLIC Code: stack=3, locals=1, args_size=1 0: aload_0 1: new // class java/lang/Object 4: dup 5: invokespecial // Method java/lang/Object."<init>":()V 8: putfield // Field obj:Ljava/lang/Object; 11: return
public int neg(int i) { return -i; } public int foo(int i) { return neg(neg(i)); } // 对应的字节码 public int foo(int); descriptor: (I)I flags: ACC_PUBLIC Code: stack=3, locals=2, args_size=2 0: aload_0 1: aload_0 2: iload_1 3: invokevirtual // Method neg:(I)I 6: invokevirtual // Method neg:(I)I 9: ireturn
foo(2)的执行过程
public void array() { int[] a = new int[10]; Object[] b = new Object[10]; int[][] c = new int[10][10]; int l = a.length; } // 对应的字节码 public void array(); descriptor: ()V flags: ACC_PUBLIC Code: stack=2, locals=5, args_size=1 0: bipush 10 2: newarray int 4: astore_1 5: bipush 10 7: anewarray #2 // class java/lang/Object 10: astore_2 11: bipush 10 13: bipush 10 15: multianewarray #3, 2 // class "[[I" 19: astore_3 20: aload_1 21: arraylength 22: istore 4 24: return
数组类型 | 加载指令 | 存储指令 |
---|---|---|
byte/boolean | baload | bastore |
char | caload | castore |
short | saload | sastore |
int | iaload | iastore |
long | laload | lastore |
float | faload | fastore |
double | daload | dastore |
reference | aaload | aastore |
返回类型 | 返回指令 |
---|---|
void | return |
int/short/char/byte/boolean | ireturn |
long | lreturn |
float | freturn |
double | dreturn |
reference | areturn |
转载请注明出处:http://zhongmingmao.me/2019/01/03/jvm-basic-bytecode/
访问原文「JVM基础 -- 字节码」获取最佳阅读体验并参与讨论