Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the performance difference, if any, between if(!foo) and if(foo == false) in Java?

Logically, if(!foo) and if(foo == false) are equivalent. How are they represented in Java? Is there any difference between the two after compilation, either in the bytecode or in performance? I was unable to find an answer in the JLS, and searching brought up a lot of results about = vs. == typos and ==/equals() behavior. (In this case, the symbols hampered my searching; for future searchers, negation operator, equals false, equal to false, not condition).

To head off the CW debate: this question is NOT asking which variant people prefer or which is considered better style. I am interested in the differences in the implementation of the language, so there is a correct answer. Related-but-not-quite-a-dupe: Difference between while (x = false) and while (!x) in Java?

EDIT:

The general consensus seems to be that a good compiler should optimize these to the same thing. That makes sense and is what I suspected, but -- to ask an even MORE academic question -- is that behavior actually mandated anywhere, or is it "merely" the reasonable thing to do?

like image 273
Pops Avatar asked Jan 11 '10 17:01

Pops


People also ask

Does or return true if both are true?

An expression using the OR operator will evaluate to TRUE if the left operand or the right operand is TRUE. If both are TRUE, the expression will evaluate to TRUE, however if neither are TRUE, then the expression will be FALSE.

In which data type the outcome is true or false?

In computer science, the Boolean (sometimes shortened to Bool) is a data type that has one of two possible values (usually denoted true and false) which is intended to represent the two truth values of logic and Boolean algebra.

Does an if statement evaluate a boolean expression?

if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false. If the condition is true, the statement is executed.

What is the connection between Boolean expressions and if statements?

The test can be any expression that evaluates to a boolean value – true or false – value (boolean expressions are detailed below). The if-statement evaluates the test and then runs the body code only if the test is true. If the test is false, the body is skipped.


1 Answers

The JLS would specify the required behavior of the statements. However, how they are implemented is an implementation detail of the compiler and the JVM.

In practice, any compiler worth its salt should emit the same bytecode for those statements. And even if not, the JVM would optimize them properly.

Also, a better way to answer this, is to check for yourself, using javap:

  1. Compile a Test.java with the following content:

    class Test {
        void equals(boolean f) {
            if (f == false) {}
        }
        void not(boolean f) {
            if (!f) {}
        }
    }
    $ javac Test.java
    
  2. De-assemble it:

    $ javap -c Test
    Compiled from "Test.java"
    class Test extends java.lang.Object{
    Test();
      Code:
       0:   aload_0
       1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
       4:   return
    
    void equals(boolean);
      Code:
       0:   iload_1
       1:   ifne    4
       4:   return
    
    void not(boolean);
      Code:
       0:   iload_1
       1:   ifne    4
       4:   return
    
    }
    

UPDATE: Responding your question about the "academic" question. As mentioned above, the JLS is only concerned with the behavior. There is nothing in the standard that actually specifies how it should be implemented (well, JVMS provides a lot of guidance).

As long as the compiler preserves the same identical behavior, the compiler is free to implement it different, with possibility different runtime performance.

like image 141
notnoop Avatar answered Sep 30 '22 05:09

notnoop