user3226268
user3226268

Reputation: 21

Difference between using unary operator ! and (== false) in if statement

I need to know what is difference between using ! operator and ==false in if statement.

I know output will be same for both. Am expecting to know internal jvm performance or any memory usage for using false value like that.

By using ! operator

if(!MyUtil.isFieldEmpty(String value)){
   System.Out.println("Not Empty");
}

By using ==false

if(MyUtil.isFieldEmpty(String value) == false){
   System.Out.println("Not Empty");
}

MyUtil.java

public static boolean isFieldEmpty(String value){
  if ((value == null) || (value.trim().length() == 0) || value.equals("null")) {
    return true;
  } else {
    return false;
  }
}

Thanks!!!

Upvotes: 2

Views: 533

Answers (7)

Makoto
Makoto

Reputation: 106440

In terms of performance, there may (or may not, likely not) be a slight edge in favor of using the simpler (that is, not including ==) form. Otherwise, they're virtually identical.

But that's really not the point here.

Writing your boolean expressions in the form of if(booleanMethod() == true|false) is entirely redundant. That decreases readability in some aspects, introduces unnecessary code and evaluation statements, and most modern IDEs/Java style checkers will complain about expressions like that.

As an example, PMD will whine about this being a case of "SimplifyBooleanExpressions".

If your left-hand expression is a boolean, and all you need is a boolean, then just use the left-hand expression. !MyUtil.isFieldEmpty(String value) is perfectly acceptable.

Also, as an aside, your boolean method could also do with some refactoring, too. It boils down to this:

public static boolean isFieldEmpty(String value) {
     return (value == null) || (value.trim().length() == 0) || value.equals("null");
}

This is for the same reason: redundancy. You're basically saying, "if this is true, or this is true, or this is true, then return true." Turns out that's just the result of the natural evaluation of the expression.

Why not just return the result of the expression? PMD would consider this a case of "SimplifyBooleanReturns".

Upvotes: 4

Rajaah
Rajaah

Reputation: 51

Both will give the same output.

In the first method,

The NOT operator(!) will do the negation of the boolean value returned.

In the second method, It will check wheather the boolean returned value is false or not.

The second method is directly checking whether the boolean returned value is false or not and in the first method it is indirectly checking for false value. Bcas, !true = false and !false = true

Upvotes: 0

earthmover
earthmover

Reputation: 4525

The ! operator simply invert your condition.

boolean a = false;
if(!a) // !a == true
   //if always runns
else
   //else never run

The == false check if your condition is false or not.

if(a == false) //since 'a = false'
   //if runs
else
  //never run

in both conditions if executes.

Upvotes: 0

Indra Yadav
Indra Yadav

Reputation: 600

! Operator just convert the true into false and false into true but ==false only check for the condition for false value

For example

boolean flag = true;
//its true
if(flag) {
}

//its false
if(!flag) {

}

flag = false ;


//its false
if(flag) {
}

//its true
if(!flag) {

}

But in ==false you can only check the false condition and it will returns true if and only if the left side value is false

boolean flag = true;
//its false
if(flag == false) {

}

flag = false;

//its true
if(flag == false){

}

Upvotes: 0

Christian Tapia
Christian Tapia

Reputation: 34146

! negates a boolean expression:

  • !true: is false
  • !false: is true

while something == false will evaluate to:

  • true: if something is false
  • false: if something is true

In this case both expressions will evaluate to the same value:

public static void main(String[] args)
{
    System.out.println(!false);
    System.out.println(false == false);
    System.out.println(!true);
    System.out.println(true == false);
}

Output:

true
true
false
false

Upvotes: 0

Rahul
Rahul

Reputation: 45070

The very simple difference is that the unary operator (!) does a negation of the boolean value returned from the method and uses that as for the if statement.

Whereas the == is the comparison operator which compares the return value of the method to false(the value specified on the RHS).

!false yields true - does negation
true == false yields false - does comparison

Upvotes: 0

mjkaufer
mjkaufer

Reputation: 4206

They're essentially the same. In any Java if statement, if you merely input a boolean, the if will compare the boolean there to true. So say I have:

boolean x = false;
if(!x)
return x;

This will return false. !x is equal to true. The same would happen for

boolean x = false;
if(x == false)
return x;

Basically, there is no difference in execution.

Upvotes: 0

Related Questions