For the following Java class:
public class ArtClass {
public boolean foo(int x) {
if(x == 3956681)
return true;
else if(x == 9855021)
return true;
else if(x == 63085561)
return true;
else
return false;
}
}
Its JVM instructions are:
I4 Branch 1 IF_ICMPNE L3
I13 Branch 2 IF_ICMPNE L5
I22 Branch 3 IF_ICMPNE L7
I understand that the first branch is in the third line and the same for the second and third branches but what does IF_ICMPNE
mean and also what do I4
, I13
, and I22
mean?
This is the output, javap -c
produced for your class (javap
is a tool that ships with each standard JDK):
Compiled from "ArtClass.java"
public class ArtClass {
public ArtClass();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public boolean foo(int);
Code:
0: iload_1
1: ldc #2 // int 3956681
3: if_icmpne 8
6: iconst_1
7: ireturn
8: iload_1
9: ldc #3 // int 9855021
11: if_icmpne 16
14: iconst_1
15: ireturn
16: iload_1
17: ldc #4 // int 63085561
19: if_icmpne 24
22: iconst_1
23: ireturn
24: iconst_0
25: ireturn
}
The meaning of all instructions has been specified in the “Instruction Set” chapter of the The Java® Virtual Machine Specification. The instruction if_icmpne will pop two int values, compare them, and branch to the specified target if not equal.
The output of javap
makes it pretty clear, which targets have been specified by the branch instructions, as they match the numbers printed before each instruction.
If you use a different tool producing different output, you have to refer to the tool’s documentation regarding, how to decipher the output. Comparing with javap
’s output suggests that these prefixes like I4
also refer to bytecode offsets, but without further context, e.g. seeing the other instructions of the method, that’s quiet meaningless.