| 前綴 | 類型 | 描述 |
| i | int | 32 位整數 |
| l | long | 64 位整數 |
| f | float | 32 位浮點 |
| d | double | 64 位浮點 |
| a | reference | 物件引用 |
| b | byte | 8 位整數 |
| c | char | 16 位字符 |
| s | short | 16 位整數 |
例:iload (int 載入)、fadd (float 加法)、astore (引用存儲)
核心指令分類大全
1. 載入/存儲指令(局部變量表 ⇄ 操作數棧)
// 載入:iload, iload_, lload, fload, dload, aload...
// 存儲:istore, istore_, lstore, fstore, dstore, astore...
// 常量:bipush, sipush, ldc, ldc_w, ldc2_w
// aconst_null, iconst_m1, iconst_, lconst_, fconst_, dconst_
// 廣域索引:wide
2. 運算指令(操作數棧頂運算)
// 算術:iadd/ladd/fadd/dadd, isub/..., imul/..., idiv/..., irem/...
// 取反:ineg/lneg/fneg/dneg
// 位移:ishl/ishr/iushr, lshl/lshr/lushr
// 位運算:ior/lor, iand/land, ixor/lxor
// 局部自增:iinc
// 比較:dcmpg/dcmpl, fcmpg/fcmpl, lcmp
異常: 整數除法/求餘除數為 0 → ArithmeticException;溢位不拋異常。
3. 類型轉換指令
// 擴寬(無損,無指令隱式):i→l/f/d, l→f/d, f→d
// 窄化(有損,顯式指令):i2b, i2c, i2s, l2i, f2i, f2l, d2i, d2l, d2f
// 窄化規則:截取低位 N 字節,可能改變符號/大小
4. 物件創建與操作
// 創建:new (實例), newarray/anewarray/multianewarray (陣列)
// 字段:getfield/putfield (實例), getstatic/putstatic (靜態)
// 陣列:*aload/*astore (載入/存儲), arraylength
// 類型檢查:instanceof, checkcast
5. 操作數棧管理
pop, pop2, dup, dup2, dup_x1, dup2_x1, dup_x2, dup2_x2, swap
6. 控制轉移
// 條件:ifeq/iflt/ifle/ifne/ifgt/ifge, ifnull/ifnonnull
// if_icmpeq/if_icmpne/if_icmplt/if_icmpgt/if_icmple/if_icmpge
// 復合:tableswitch, lookupswitch
// 無條件:goto/goto_w, jsr/jsr_w, ret
// 比較策略:所有類型最終轉為 int 比較;long/float/double 先比較產生 int 結果
7. 方法調用與返回
// 調用:invokevirtual (虛方法), invokeinterface (介面),
// invokespecial (構造/私有/父類), invokestatic (靜態)
// 返回:ireturn (int/boolean/byte/char/short), lreturn, freturn, dreturn, areturn, return (void)
8. 同步指令
// 方法級:ACC_SYNCHRONIZED 標誌,隱式 monitorenter/monitorexit
// 代碼塊:monitorenter, monitorexit
// 結構化鎖規則:
// 1. 持有次數 = 釋放次數
// 2. 任意時刻 釋放次數 ≤ 持有次數
進階話題
invokedynamic (Java 7+)
支援動態語言,Lambda 表達式底層基石,透過 CallSite + MethodHandle 實現啟動時綁定。
字節碼驗證器
類載入階段驗證:格式、元數據、字節碼流、符號引用,確保類型安全、棧平衡、跳轉目標合法。
實用工具
javap -c ClassName:反編譯查看字節碼
- ASM / ByteBuddy:字節碼操作框架
- JIT 編譯器:熱點字節碼 → 機器碼
- 上一篇文章: Java 反射機制深度解析:從 Class 對象到動態創建、結構探測與屬性/方法操作實戰
- 下一篇文章: Java 動態代理深度實戰:JDK Proxy 機制、InvocationHandler 與生成類結構解析
Copyright ©
程式碼之家 All Rights Reserved