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.