When should I not use the Java Shortcut Operator &

女生的网名这么多〃 提交于 2019-12-12 12:14:45

问题


My understanding is that when using && as an mathematical operation in Java, if the LHS (Left Hand Side) of an evaluation fails, the right hand side will not be checked, so.

false && getName();

getName() would never be called as the LHS has already failed.

false & getName();

When would I ever want to check the RHS if I know the LHS has failed? Surely any dependency on the RHS evaluation being ran would be bad coding practice?

Thank you


回答1:


Well the reason for & and && is not, that one evaluates the right side and the other doesn't.

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

    Theoretically, if all bits of the left side are 0 (as is the case for false), the right side must not neccessarily be evaluated. I guess it's a design decision that it is evaluated in every case. In the simple cases, it is faster than checking if the left side is 0.

  • && is a conditional logical operator, which takes two booleans and returns a boolean (that is true if and only if both sides are true).

    The same applies here, if the left side is false, we don't need to check the right side. Here the decision was taken to skip the evaluation of the right side in that case.

And to answer your last question (when would you want to evaluate the RHS if the LHS has failed), there are some scenarios where it can be ok. However, in any case it is possible to prevent these situations (see jocelyn's answer for a good way to make sure that both expressions are evaluated) without loosing readability. In fact, I think jocelyn's way is more readable than if (exprA() & exprB()) { ... }.

Personally I never use & unless I really need a bitwise AND.




回答2:


When should I not use the Java Shortcut Operator &

That's a strange question. The best answer I can think of is that you should not use it if you DO want the short-circuit behaviour of &&. (And that is most of the time ...)

Surely any dependency on the RHS evaluation being ran would be bad coding practice?

I disagree. That is tantamount to saying that side-effects are bad coding practice.

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




回答3:


It is used in bitwise ANDing

for example

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 them then you could use




回答4:


Another typical case of use is you want to check both computed conditions:

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

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

Bear in mind that this is achievable with another methods. It saves you a couple of lines of code. Also as a language is nice to give the chance of evaluate both. Otherwise someone could ask why I cannot.




回答5:


Indeed it would be bad practice to rely on the call of a RHS. If you need it to be called each time, call it before, store the result and use it in your comparaison expression.

if (false && getresult()) {

}

becomes

Boolean result = getresult();
if (false && result) {

}


来源:https://stackoverflow.com/questions/11135715/when-should-i-not-use-the-java-shortcut-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!