1

I saw java bytecode that need to compare to var, if they equal return true, else .....

if(var1==var2)
   return true;
else
{ ...}

on bytecode

aload 1
aload 2
if_acmpne 6
iconst 1
ireturn

I understand iconst1 and ireturn to return true. and understand aload1 and aload2 to load 2 var to stack.

But how if_acmpne 6 works? what is 6? thank

2

1 Answer 1

1

The if_acmpne instruction pops two objects off the stack and compares them. If they are not equal, then it jumps to the provided offset, which in this case is 6 bytes from the start of the method code.

In general, you can learn about all of the bytecode instructions by reading the JVM specification here: https://docs.oracle.com/javase/specs/

4
  • in this case 6 is that mean instruction no..6 from the beginning of my function? is that depend on my os or on somthing else? or 6 is always instruction no. 6?
    – cdcsccd
    Feb 15, 2018 at 3:37
  • Instruction sizes are defined by the bytecode format and are OS independent. Most instructions are one byte long (hence the term bytecode), but some are longer. if_acmpne itself is 3 bytes for instance. Again, you really need to read the JVM spec.
    – Antimony
    Feb 15, 2018 at 14:53
  • I read the JVM spec, but when i open this class with Krakatau that show that the number that come after if_acmpne define lable name
    – cdcsccd
    Feb 15, 2018 at 17:59
  • @cdcsccd That's the assembler being helpful. Working with the raw bytes directly is a huge pain, so assemblers define a human readable format which can be mapped to and from the underlying binary classfile. If you want to see the raw classfile, use a hex editor.
    – Antimony
    Feb 15, 2018 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.