java - 短路运算符是否过载?

标签 java or-operator

使用位运算符有两种情况:

对于 boolean 值

boolean a = true;
boolean b= false;
boolean c = a|b; // Giving response after logical OR for booleans.

对于整数

int a = 10;
int b = 20;
int c = a|b;  // Giving response after bitwise OR for boolean equivalents of "a" and "b".

以上两种情况均符合http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.22.2 .

运算符|是否过载?

我只想问一个非常简单的问题:是“|”重载还是它对 boolean 值(当然是二进制等价物)和整数执行相同的按位或任务?

最佳答案

您可能会找到答案 here :

When both operands of a &, ^, or | operator are of type boolean or Boolean, then the type of the bitwise operator expression is boolean. In all cases, the operands are subject to unboxing conversion (§5.1.8) as necessary.

如果你在 boolean 上使用 | 运算符,那么结果就像使用 || 但请注意不同之处在于案例:

boolean a = true, b = false;
boolean c = a | b; //b will still be evaluated
c = a || b;        //b will not be evaluated

我不确定询问它是否重载是什么意思,因为它可以用于不同的类型,所以它是重载的。

关于java - 短路运算符是否过载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17337640/

相关文章:

c - c 中二进制字符串的 OR 运算给出一半输出错误

c# - OR 正则表达式中的表达式?

Javascript:如何在 if 构造中获得变量可以等于的可能值列表

java - 如何使用二分查找找到用户编号

java - while 后无法到达的语句

java - 我应该使用哪种 java swing 布局

java - 不幸的是 ,"App"已经停止,OnClickListener

python - 检查数字零时的多个条件声明 - python

javascript - 由 OR 运算符分隔的语句的求值顺序

java - 如何编写在实体实例上同步的 Java 代码