5.2 Comparing References

The third group of instructions is used to compare references to see if they are equal (if_acmpeq) or not equal (if_acmpne). Two references are equal only if they refer to the same object or if they are both null. For example,

 aload_0 aload_0 if_acmpeq success           ; This test always goes to success,                             ; since the same reference is loaded                             ; twice aload_1 dup if_acmpeq success           ; This test also goes to success,                             ; since the duped value is equal                             ; to the original new Cookie dup invokevirtual Cookie/<init> ()V   ; Make a cookie new Cookie dup invokevirtual Cookie/<init> ()V   ; Make another one if_acmpeq success                 ; This comparison fails, since                                   ; it compares two different                                   ; Cookies new Cookie() dup invokevirtual Cookie/<init>()V    ; Make yet another cookie aconst_null                       ; Put null on the stack if_acmpeq nullcookie              ; This test fails, because                                   ; no object is equal to null                                   ; Get the chocolate chip                                   ; cookie getstatic Cookie/chocolateChip LCookie; aconst_null if_acmpeq nocookie                ; If the chocolateChip Cookie is                                   ; null, go to nocookie 

Since comparison to null is something JVM programs do frequently, there are two special instructions (ifnull and ifnonnull) that compare a single object to null. The last example can be rewritten

 getstatic Cookie/chocolateChip LCookie; ifnull nocookie                   ; If the chocolateChip Cookie is                                   ; null, go to nocookie 

Table 5.1 summarizes the branching instructions.

Table 5.1. Branch instructions
Mnemonic Argument Stack Effect
goto label   Control transfers to label.
if_acmpeq label ref1 ref2 If ref1 references the same object as ref2, control branches to label.
if_acmpne label ref1 ref2 If ref1 does not reference the same object as ref2, control branches to label.
ifnonnull label ref1 If ref1 is not null, control branches to label.
ifnull label ref2 If ref1 is null, control branches to label.
if_icmpeq label int1 int2 If int1 == int2, control branches to label.
if_icmpne label int1 int2 If int1!= int2, control branches to label.
if_icmplt label int1 int2 If int1 < int2, control branches to label.
if_icmpge label int1 int2 If int1 >= int2, control branches to label.
if_icmpgt label int1 int2 If int1 >= int2, control branches to label.
if_icmple label int1 int2 If int1 <= int2, control branches to label.
ifeq label int1 If int1 == 0, control branches to label.
ifne label int1 If int1!= 0, control branches to label.
iflt label int1 If int1 < 0, control branches to label.
ifge label int1 If int1 >= 0, control branches to label.
ifgt label int1 If int1 > 0, control branches to label.
ifle label int1 If int1 <= 0, control branches to label.



Programming for the Java Virtual Machine
Programming for the Javaв„ў Virtual Machine
ISBN: 0201309726
EAN: 2147483647
Year: 1998
Pages: 158
Authors: Joshua Engel

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net