2

I have the following Java class:

public class ArtClassInt {
   public boolean foo(int x) {
      if(x == 3956681)
        return true;
      else
        return false;
   }

   public boolean boo(int x) {
      if(x <= 952140568)
        return true;
      else
        return false;
   }

   public boolean boo1(int x, int y) {
      if(x <= y)
        return true;
      else
        return false;
   }

   public boolean zoo(int x) {
      if(x+1 < 1267)
        return true;
      else
        return false;
   }
}

When I complied it and got its bytecode, I got the following statements that correspond to the if-statements in the source code:

ArtClassInt.boo1(II)Z: I4 Branch 3 IF_ICMPGT L17 - true
ArtClassInt.boo1(II)Z: I4 Branch 3 IF_ICMPGT L17 - false
ArtClassInt.boo(I)Z: I4 Branch 2 IF_ICMPGT L10 - true
ArtClassInt.boo(I)Z: I4 Branch 2 IF_ICMPGT L10 - false
ArtClassInt.foo(I)Z: I4 Branch 1 IF_ICMPNE L3 - true
ArtClassInt.foo(I)Z: I4 Branch 1 IF_ICMPNE L3 - false
ArtClassInt.zoo(I)Z: I6 Branch 4 IF_ICMPGE L24 - true
ArtClassInt.zoo(I)Z: I6 Branch 4 IF_ICMPGE L24 - false

I am totally confused about the mnemonics (i.e., IF_ICMPGE, IF_ICMPNE, etc). By looking at the source code, I expect that:

  • The if-statement in the foo method should be if_icmpeq not IF_ICMPNE
  • The if-statement in the boo and boo1 methods should be if_icmple not IF_ICMPGT.
  • The one in zoo method should be if_icmplt not IF_ICMPGE.

Can someone please explain the reason behind that?

3
  • 3
    By the way, instead of writing if(x <= 952140568) return true; else ... you can just write this: return x <= 952140568. What you're doing is a common beginners mistake. Commented Feb 21, 2019 at 14:54
  • Not really a mistake (the code works fine) but rather non-idiomatic code. Commented Feb 21, 2019 at 15:34
  • "The if-statement in the foo method should be if_icmpeq not IF_ICMPNE" -> why?!! "The if-statement in the boo and boo1 methods should be if_icmple not IF_ICMPGT" --> why?!! Its compilers choice what construct to use to create a byte code that resembles your logic. exchanging does not change anything in the logic only in the opcode.. Btw don't asume to find every single comparison or "loop"(branch) in the bytecode. Commented Feb 21, 2019 at 15:55

2 Answers 2

2

The condition of the test is inverted because it does a jump (to the else block) if the condition is true, otherwise it continues to the next instruction.

Sign up to request clarification or add additional context in comments.

Comments

2

I guess you shouldn't assume that your statements should be converted verbatim to bytecode.

The only relevant thing is that the semantics should be the same. It's common for conditions to be inverted to counterparts in intermediate code, think about a while loop:

while (x < y) {
  code;
}

could be compiled as

START:
  CMP x y
  JMPGE END
  CODE
  JMP START
END:
  ...

which you see is inverting the condition, this is a common idiom. This because an inverted condition allows you to jump out of a block, so executing it only when the opposite is true.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.