gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 02:11:53 29 4
gpt4 key购买 nike

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

对于 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com