do | and & make any sense in java?

夙愿已清 提交于 2019-12-07 07:05:46

问题


Rarely I bump into & and | logic operators in other's codes instead of || and &&. I made a quick research because I never used them and didn't know what is it for.
A && B means if A is false, B won't be evaluated and it will return false.
A & B means if A is false, B will be evaluated even if the form will return false as well.
Of course it is the same game with | and ||.

My question is: does it make sense anyways to evaluate the second member if the first determine the evaluation? I can imagine a code where the second member does something super important logic in clode and so it should be evaluated, but I suspect it is bad practice. Is it enough to make | and & exist?

from a comment, to make it more clear: I have the feeling that the program is not optimal and working well or better to say: designed well if all of the logical members HAS to be evaluated.


回答1:


You forgot about bitwise operations

The bitwise & operator performs a bitwise AND operation.

The bitwise | operator performs a bitwise inclusive OR operation.

But here you can find example when unconditional AND operator is better:

What are the cases in which it is better to use unconditional AND (& instead of &&)




回答2:


The single & and | are not logical operators, they're bitwise and logical operators.




回答3:


From Bitwise and Bit Shift Operators

The bitwise & operator performs a bitwise AND operation.
The bitwise ^ operator performs a bitwise exclusive OR operation.
The bitwise | operator performs a bitwise inclusive OR operation.

From JLS 15.22. Bitwise and Logical Operators

The bitwise operators and logical operators include the AND operator &, exclusive OR operator ^, and inclusive OR operator |. Blockquote




回答4:


You can use the | and & operators as you said above. Some static code analyzers will warn you because it might mask a possible problem. In those cases where you really want both conditions evaluated, you can do something like:

boolean cond1=cond1();
boolean cond2=cond2();
boolean myCond=cond1 && cond2;

if you just use

boolean myCond=cond1()&cond2();

someone is bound to "correct" it for you. So yes, there are places where you would use & and |, but most likely it's not a good idea and you can get around it for the sake of clarity.




回答5:


Imagine if B was a function that you wanted to execute all the time then it would be useful.




回答6:


I think that might be quite good example:

if (initializeConnectionA() & initializeConnectionB() & initializeConnectionC()) {
    performOperation();
} else {
    logger.warn("Not all modules are working properly");
}

Where methods initializeConnection connects to i.e. some external servers that might not all be working. You might not require to initialize them all but you want to be warned if some of them are not working.

Of course it might not be the clearest solution for this problem, but this is example where & operator might be useful.



来源:https://stackoverflow.com/questions/15089890/do-and-make-any-sense-in-java

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