Index Previous Next

Explicit test of boolean

Consider the following code:

   public class test {
       public boolean b ;

       public void f() {
       }

       public void g() {
           if ( b == true )
               f() ;

           if ( b )
               f() ;

           if ( b == false )
               f() ;

           if ( !b )
               f() ;
       }
   }

The expressions b == true and b are equivalent, as are b == false and !b. However, the compiler generates different code for these equivalent tests:

       b == true                    b

       aload_0                      aload_0
       getfield test/b Z            getfield test/b Z
       iconst_1                     ifeq Label2
       if_icmpne Label1


       b == false                   !b

       aload_0                      aload_0
       getfield test/b Z            getfield test/b Z
       iconst_0                     ifne Label4
       if_icmpne Label3

Similar results are seen if the == is replaced by !=. Clearly, we can replace the less efficient explicit comparison with the more efficient implicit one.

Suitable rules can be generated for various different means of obtaining the boolean value: from an instance variable, from a class variable or as the return value of a method call. The awkward case is where the boolean is obtained from a local variable. The assembly language doesn't distinguish between a boolean local variable and other integral types: in all cases the iload instruction is used. An explicit comparison against zero means the same whether the variable is a boolean or an integer. This is not so for an explicit comparison against one.


Index Previous Next