做|和&在java中有意义吗? [英] do | and & make any sense in java?

查看:231
本文介绍了做|和&在java中有意义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很少碰到&和|其他代码中的逻辑运算符而不是||和&&我做了一个快速的研究,因为我从来没有使用它们,也不知道它是什么。

A&& B表示如果A为假,则不会评估B,并且它将返回false。

A& B表示如果A为假,即使表格也会返回false,也会评估B.

当然这是同一游戏|和||。

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 ||.

我的问题是:如果第一个成员首先确定评估,那么评估第二个成员是否有意义?我可以想象一个代码,其中第二个成员在clode中执行一些非常重要的逻辑,因此应该对其进行评估,但我怀疑这是不好的做法。是否足以使|和&从评论中存在?

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.

推荐答案

你忘记了按位操作


按位&运算符执行按位AND运算。

The bitwise & operator performs a bitwise AND operation.

按位|运算符执行按位包含OR运算。

The bitwise | operator performs a bitwise inclusive OR operation.

但是在这里你可以找到无条件AND运算符更好的例子:

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

使用无条件AND(&而不是&&)更好的情况

这篇关于做|和&在java中有意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆