【发布时间】:2026-01-12 22:55:01
【问题描述】:
我正在使用 Java 类文件和字节码。但我坚持类文件中的条件。 理论上,我理解这个概念,但我不明白类文件中的分支是如何完成的。这是一个小例子:
public static void main(String[] args) {
int a = 78;
int b = 52;
boolean c;
if(a==b){
c = true;
} else {
c = false;
}
}
使用 javap -c -verbose Equal.class 会出现以下清单:
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=4, args_size=1
0: bipush 78
2: istore_1
3: bipush 52
5: istore_2
6: iload_1
7: iload_2
8: if_icmpne 16
11: iconst_1
12: istore_3
13: goto 18
16: iconst_0
17: istore_3
18: return
LineNumberTable:
line 4: 0
line 5: 3
line 7: 6
line 8: 11
line 10: 16
line 11: 18
StackMapTable: number_of_entries = 2
frame_type = 253 /* append */
offset_delta = 16
locals = [ int, int ]
frame_type = 252 /* append */
offset_delta = 1
locals = [ int ]
现在我正在查看类文件,以找到分支。操作码 if_icmpne 的十六进制表示是 0xA0。我假设分支标记将跟随 0xA0。就我而言,有 2 个字节:0x0008。 我的问题:这两个字节(0x0008)代表什么? 我尝试了很多。例如,我沿着 LineNumberTable 和常量池的路径,但找不到任何有意义的东西。
(当然 goto 也一样)
此外,这里是上面 postet 列表的完整序列:
10 4E // bipush 78
3C // istore_1
10 34 // bipush 52
3D // istore_2
1B // iload_1
1C // iload_2
A0 // if_icmpne
00 08 // ???
04 // iconst_1
3E // istore_3
A7 // goto
00 05 // ???
03 // iconst_0
3E // istore_3
B1 // return
提前谢谢你!
【问题讨论】:
标签: java branch bytecode .class-file conditional