What is the difference between these 2 ifs?

if(loadFactor != null && !(trialExists(loadFactor))

and

if(!(loadFactor == null || trialExists(loadFactor))

I was asked in the review to make the 1st, if I change to the 2nd. How does it matter.

+4
source share
4 answers

There is no difference.

In both cases:

  • If loadFactornull, trialExists(loadFactor)it will not be evaluated, and the condition will be false, so it will not go into the bodyif
  • Otherwise, it trialExists(loadFactor)will be evaluated, and the condition will be the opposite of the result - therefore, it will only enter the body of the operator ifif it trialExists(loadFactor)returns false

This is due to the fact that both &&are ||short-circuited:

  • If the first operand &&is equal false, the second operand is not evaluated, and the result isfalse
  • If the first operand ||is equal true, the second operand is not evaluated, and the result istrue

. : " , ". , " ", , , .

+5

, De Morgan , Java.

"not (A B)" "( A) ( B)"

"not (A B)" "( A) ( B)"

( )

  • , :

    loadFactor != null && !(trialExists(loadFactor)
    
  • :

    !!(loadFactor != null && !(tiralExists(loadFactor))
    
  • ( (A B) ( A) ( B))

    !(!(loadFactor != null) || !!(trialExists(loadFactor)))
    
  • :

    !(loadFactor == null || trialExists(loadFactor))
    

2- .

+6

. , loadFactor Null , , .

, , loadFactor Null.

+1

, . && secnnd arg, , || second-arg, arg .

, , , .

, , trialExists() .

+1

All Articles