6

Given this Java code:

int fst = 5;
int snd = 6;
if(fst == snd)
 do something;

I want to know how Java will compare equality for this case. Will it use an XOR operation to check equality?

quantum
3,8881 gold badge32 silver badges51 bronze badges
asked Jul 13, 2012 at 21:01
6
  • 8
    No, it will perform a if_icmpeq JVM bytecode instruction. Commented Jul 13, 2012 at 21:03
  • @jsn That sounds like a good answer to me (well, perhaps ever so slightly expanded). Commented Jul 13, 2012 at 21:05
  • The == operation translates to the if_icmpeq instruction in bytecode; during execution, this pops off the object references from the operand stack and compares them. Commented Jul 13, 2012 at 21:16
  • I also want to add that (and I know you didn't ask this) if you compare Integers (capital I, spelled out), which are objects to eachother using ==, that compares whether they're the same instance instead of whether the value of the two Integers are the same. So you should use .equals for those. Commented Jul 13, 2012 at 21:21
  • if_icmpeq pops the top two ints off the stack and compares them. If the two integers are equal, execution branches to the address (pc + branchoffset), where pc is the address of the if_icmpeq opcode in the bytecode and branchoffset is a 16-bit signed integer parameter following the if_icmpeq opcode in the bytecode. If the integers are not equal, execution continues at the next instruction. Commented Jul 13, 2012 at 21:30

2 Answers 2

10

Are you asking "what native machine code does this turn into?"? If so, the answer is "implementation-depdendent".

However, if you want to know what JVM bytecode is used, just take a look at the resulting .class file (use e.g. javap to disassemble it).

answered Jul 13, 2012 at 21:03
Sign up to request clarification or add additional context in comments.

3 Comments

+1 Absolutely! If one does not leave a decision like "how to implement int equality" up to the writers of JVM, he/she might as well write the JVM for them :)
Thanks a lot. I found what I require.
It is worthwhile to add that you can see even the native code that the JIT compiler produces. There you'll finally be able to witness your XOR or CMP or whatever the == happens to turn into.
4

In case you are asking about the JVM, use the javap program.

public class A {
 public static void main(String[] args) {
 int a = 5;
 System.out.println(5 == a);
 }
}

Here is the disassembly:

public class A {
 public A();
 Code:
 0: aload_0
 1: invokespecial #1 // Method java/lang/Object."<init>":()V
 4: return
 public static void main(java.lang.String[]);
 Code:
 0: iconst_5
 1: istore_1
 2: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
 5: iconst_5
 6: iload_1
 7: if_icmpne 14
 10: iconst_1
 11: goto 15
 14: iconst_0
 15: invokevirtual #3 // Method java/io/PrintStream.println:(Z)V
 18: return
}

In this case it optimized the branching a bit and used if_icmpne. In most cases, it will use if_icmpne or if_icmpeq.

if_icmpeq : if ints are equal, branch to instruction at branchoffset (signed short constructed from unsigned bytes branchbyte1 << 8 + branchbyte2)

if_icmpn : if ints are not equal, branch to instruction at branchoffset (signed short constructed from unsigned bytes branchbyte1 << 8 + branchbyte2)

answered Jul 13, 2012 at 21:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.