To do | and & makes sense in java?

Rarely do I encounter and and | logical operators in other codes instead of || and & &. I did a quick research because I never used them and did not know what it was for. A && B means that if A is false, B will not be evaluated and it will return false.
A and B means that if A is false, B will be evaluated even if the form also returns false.
Of course, this is the same game with | and ||.

My question is: does it make sense in any case to evaluate the second member if the first determines the grade? I can imagine a code in which the second member does something super important logic in the clod, and therefore it should be evaluated, but I suspect that this is bad practice. Is it enough to do | and exist?

from the commentary, to make it clearer: I have a feeling that the program is not optimal and works well, or better said: well designed if all logical members should be evaluated.

+4
source share
6 answers

You forgot about bitwise operations

Bitwise and the operator performs a bitwise AND operation.

Bitwise | the operator performs bitwise inclusion of the OR operation.

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

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

+6
source

Single and | are not logical operators, they are bitwise and logical operators.

+5
source

From Bitwise and Bit Shift Operators

Bitwise and the operator performs a bitwise AND operation.
The bitwise ^ operator performs a bitwise exclusive OR operation. Bitwise | the operator performs bitwise inclusion of the OR operation.

From JLS 15.22. Bitwise and logical operators

Bitwise operators and logical operators include the AND & operator, the exclusive OR ^ operator, and the including OR operator. Blockquote

enter image description here

+2
source

You can use | and operators, as you said above. Some static code analyzers will warn you because this may mask a possible problem. In cases where you really want both conditions to be 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 required to “fix” it for you. So yes, there are places where you would use and and |, but most likely this is not a good idea, and you can get around this for the sake of clarity.

+2
source

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

+2
source

I think this may be a good example:

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

Where initializeConnection methods connect, for example, to some external servers that may not work. You may not need to initialize all of them, but you want to be warned if some of them do not work.

Of course, this may not be the clearest solution to this problem, but this is an example where the operator can be useful.

+2
source

All Articles