When should I not use Java Shortcut Operator &

I understand that when using && as a mathematical operation in Java, if the LHS (left side) evaluation is not performed, the right side will not be checked, therefore.

false && getName(); 

getName () will never be called because LHS has already failed.

 false & getName(); 

When will I ever want to test RHS if I know that LHS failed? Surely any reliance on RHS rating would be bad coding practice?

thanks

+4
source share
5 answers

Well, the reason for & and && not the one that evaluates the right side, but the other not.

  • & is a bitwise operator , which does the bitwise AND of two values. It returns a value.

    Theoretically, if all the bits of the left side are 0 (as is the case with false ), the right side does not have to be evaluated. I guess this is a design decision that is being evaluated in each case. In simple cases, this is faster than checking if the left side is 0.

  • && is a conditional logical operator that takes two logical values ​​and returns a logical value (i.e. true if and only if both sides are true ).

    The same applies here, if the left side is false , we do not need to check the correct side. Here it was decided to skip the assessment of the right side in this case.

And to answer your last question (when you want to evaluate RHS if LHS failed), there are some scenarios where this might be good. However, in any case, you can prevent these situations (see jocelyn answer to make sure that both expressions are evaluated) without losing readability. In fact, I think the jocelyn path is more readable than if (exprA() & exprB()) { ... } .

Personally, I never use & unless I really need bitwise I.

+5
source

When should I not use Java Shortcut Operator &

This is a strange question. The best answer I can come up with is that you shouldn't use it if you need the && short circuit behavior. (And this is in most cases ...)

Surely any reliance on an ongoing RHS assessment would be bad coding practice?

I disagree. This is equivalent to side effects being bad coding practice.

I will give you that you rarely need to use & in boolean expressions, but that does not make it a bad practice to use them.

+2
source

Used in beaten AND

eg

 int i = 3; // 11 - binary int j = 7; / 111 - binary int k = i &j; // 11 & 111 = 11 = 3 (decimal) 

basically does logical ANDing, so if you want to evaluate both the condition and then AND, you can use

+1
source

In another typical use case, you want to check both calculated conditions:

 if(++i < 0 & ++j < 0) 

In this example, regardless of the value of i j , increases.

Keep in mind that this is possible using other methods. This will save you a couple of lines of code. Also, like a language, it's nice to give an opportunity to evaluate both. Otherwise, someone might ask why I cannot.

+1
source

Indeed, it would be bad practice to rely on the call of the RHS. If you need it to be called every time, call it earlier, save the result and use it in the comparaison expression.

 if (false && getresult()) { } 

becomes

 Boolean result = getresult(); if (false && result) { } 
+1
source

All Articles