Java 虛擬機指令由單字節操作碼(Opcode)與零至多個操作數組成。熟悉指令集有助於閱讀字節碼、編寫字節碼操作工具、優化性能及排查疑難問題。下表按功能分類匯總所有標準指令(基於 JVM Spec 8)。
| Opcode | 助記符 | 說明 |
|---|---|---|
| 0x00 | nop | 無操作 |
| 0x01 | aconst_null | 將 null 壓入棧 |
| 0x02 | iconst_m1 | 將 int -1 壓入棧 |
| 0x03~0x08 | iconst_0 ~ iconst_5 | 將 int 0~5 壓入棧 |
| 0x09~0x0A | lconst_0, lconst_1 | 將 long 0/1 壓入棧 |
| 0x0B~0x0D | fconst_0 ~ fconst_2 | 將 float 0.0~2.0 壓入棧 |
| 0x0E~0x0F | dconst_0, dconst_1 | 將 double 0.0/1.0 壓入棧 |
| 0x10 | bipush | 將單字節常量(-128~127)壓入棧 |
| 0x11 | sipush | 將 short 常量(-32768~32767)壓入棧 |
| 0x12 | ldc | 將常量池 int/float/String 壓入棧 |
| 0x13 | ldc_w | 寬索引版 ldc |
| 0x14 | ldc2_w | 將常量池 long/double 壓入棧(寬索引) |
| Opcode | 助記符 | 說明 |
|---|---|---|
| 0x15 | iload | 將指定索引 int 變量壓入棧 |
| 0x16 | lload | 將指定索引 long 變量壓入棧 |
| 0x17 | fload | 將指定索引 float 變量壓入棧 |
| 0x18 | dload | 將指定索引 double 變量壓入棧 |
| 0x19 | aload | 將指定索引引用變量壓入棧 |
| 0x1A~0x1D | iload_0 ~ iload_3 | 將第 1~4 個 int 變量壓入棧(隱式索引) |
| 0x1E~0x21 | lload_0 ~ lload_3 | 將第 1~4 個 long 變量壓入棧 |
| 0x22~0x25 | fload_0 ~ fload_3 | 將第 1~4 個 float 變量壓入棧 |
| 0x26~0x29 | dload_0 ~ dload_3 | 將第 1~4 個 double 變量壓入棧 |
| 0x2A~0x2D | aload_0 ~ aload_3 | 將第 1~4 個引用變量壓入棧 |
| Opcode | 助記符 | 說明 |
|---|---|---|
| 0x2E | iaload | int 數組加載 |
| 0x2F | laload | long 數組加載 |
| 0x30 | faload | float 數組加載 |
| 0x31 | daload | double 數組加載 |
| 0x32 | aaload | 引用數組加載 |
| 0x33 | baload | byte/boolean 數組加載 |
| 0x34 | caload | char 數組加載 |
| 0x35 | saload | short 數組加載 |
| Opcode | 助記符 | 說明 |
|---|---|---|
| 0x36 | istore | 棧頂 int 存入指定索引 |
| 0x37 | lstore | 棧頂 long 存入指定索引 |
| 0x38 | fstore | 棧頂 float 存入指定索引 |
| 0x39 | dstore | 棧頂 double 存入指定索引 |
| 0x3A | astore | 棧頂引用存入指定索引 |
| 0x3B~0x3E | istore_0 ~ istore_3 | 棧頂 int 存入第 1~4 個局部變量 |
| 0x3F~0x42 | lstore_0 ~ lstore_3 | 棧頂 long 存入第 1~4 個局部變量 |
| 0x43~0x46 | fstore_0 ~ fstore_3 | 棧頂 float 存入第 1~4 個局部變量 |
| 0x47~0x4A | dstore_0 ~ dstore_3 | 棧頂 double 存入第 1~4 個局部變量 |
| 0x4B~0x4E | astore_0 ~ astore_3 | 棧頂引用存入第 1~4 個局部變量 |
| Opcode | 助記符 | 說明 |
|---|---|---|
| 0x4F | iastore | int 數組存儲 |
| 0x50 | lastore | long 數組存儲 |
| 0x51 | fastore | float 數組存儲 |
| 0x52 | dastore | double 數組存儲 |
| 0x53 | aastore | 引用數組存儲 |
| 0x54 | bastore | byte/boolean 數組存儲 |
| 0x55 | castore | char 數組存儲 |
| 0x56 | sastore | short 數組存儲 |
| Opcode | 助記符 | 說明 |
|---|---|---|
| 0x57 | pop | 彈出棧頂(非 long/double) |
| 0x58 | pop2 | 彈出棧頂 long/double 或兩個普通值 |
| 0x59 | dup | 復制棧頂值 |
| 0x5A | dup_x1 | 復制棧頂並插入次棧頂 |
| 0x5B | dup_x2 | 復制棧頂並插入第三位置 |
| 0x5C | dup2 | 復制棧頂兩個值 |
| 0x5D | dup2_x1 | 復制棧頂兩值並插入 |
| 0x5E | dup2_x2 | 復制棧頂兩值並插入 |
| 0x5F | swap | 交換棧頂兩值 |
| Opcode | 助記符 | 說明 |
|---|---|---|
| 0x60~0x63 | iadd, ladd, fadd, dadd | 加法 |
| 0x64~0x67 | isub, lsub, fsub, dsub | 減法 |
| 0x68~0x6B | imul, lmul, fmul, dmul | 乘法 |
| 0x6C~0x6F | idiv, ldiv, fdiv, ddiv | 除法 |
| 0x70~0x73 | irem, lrem, frem, drem | 取模 |
| 0x74~0x77 | ineg, lneg, fneg, dneg | 取負 |
| 0x78~0x7D | ishl, lshl, ishr, lshr, iushr, lushr | 位移操作 |
| 0x7E~0x83 | iand, land, ior, lor, ixor, lxor | 位運算 |
| 0x84 | iinc | 局部變量自增 |
| Opcode | 助記符 | 說明 |
|---|---|---|
| 0x85~0x93 | i2l, i2f, i2d, l2i, l2f, l2d, f2i, f2l, f2d, d2i, d2l, d2f, i2b, i2c, i2s | 數值類型轉換(寬化/窄化) |
| Opcode | 助記符 | 說明 |
|---|---|---|
| 0x94 | lcmp | long 比較,結果 -1/0/1 入棧 |
| 0x95~0x96 | fcmpl, fcmpg | float 比較,NaN 處理不同 |
| 0x97~0x98 | dcmpl, dcmpg | double 比較,NaN 處理不同 |
| 0x99~0x9E | ifeq, ifne, iflt, ifge, ifgt, ifle | int 與 0 比較跳轉 |
| 0x9F~0xA4 | if_icmpeq ~ if_icmple | 兩 int 比較跳轉 |
| 0xA5~0xA6 | if_acmpeq, if_acmpne | 引用比較跳轉 |
| 0xA7 | goto | 無條件跳轉 |
| 0xA8 | jsr | 跳轉子程序(已棄用) |
| 0xA9 | ret | 返回子程序(已棄用) |
| 0xAA | tableswitch | switch 連續 case |
| 0xAB | lookupswitch | switch 非連續 case |
| 0xAC~0xB0 | ireturn, lreturn, freturn, dreturn, areturn, return | 各類型返回 |
| Opcode | 助記符 | 說明 |
|---|---|---|
| 0xB2 | getstatic | 獲取靜態字段 |
| 0xB3 | putstatic | 設置靜態字段 |
| 0xB4 | getfield | 獲取實例字段 |
| 0xB5 | putfield | 設置實例字段 |
| 0xB6 | invokevirtual | 調用實例方法 |
| 0xB7 | invokespecial | 調用構造器/私有/父類方法 |
| 0xB8 | invokestatic | 調用靜態方法 |
| 0xB9 | invokeinterface | 調用接口方法 |
| 0xBA | invokedynamic | 調用動態方法 |
| 0xBB | new | 創建對象 |
| 0xBC | newarray | 創建基本類型數組 |
| 0xBD | anewarray | 創建引用類型數組 |
| 0xBE | arraylength | 獲取數組長度 |
| 0xBF | athrow | 拋出異常 |
| 0xC0 | checkcast | 類型檢查轉換 |
| 0xC1 | instanceof | 實例檢查 |
| 0xC2 | monitorenter | 進入同步塊 |
| 0xC3 | monitorexit | 退出同步塊 |
| Opcode | 助記符 | 說明 |
|---|---|---|
| 0xC4 | wide | 擴展局部變量索引寬度 |
| 0xC5 | multianewarray | 創建多維數組 |
| 0xC6 | ifnull | 為 null 跳轉 |
| 0xC7 | ifnonnull | 非 null 跳轉 |
| 0xC8 | goto_w | 寬索引無條件跳轉 |
| 0xC9 | jsr_w | 寬索引子程序跳轉 |
| 0xCA | breakpoint | 調試斷點 |
| 0xFE | impdep1 | 硬件相關保留 |
| 0xFF | impdep2 | 硬件相關保留 |
javap -c ClassName 查看編譯後字節碼。wide 前綴指令會將後續指令的索引從 16 位擴展為 32 位。