Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

do | and & make any sense in java?

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.

like image 874
CsBalazsHungary Avatar asked Dec 01 '22 21:12

CsBalazsHungary


1 Answers

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 &&)

like image 81
bsiamionau Avatar answered Dec 09 '22 13:12

bsiamionau