有人能解释一下Java中OR运算符(
例如:
||
和|
)的区别和用法吗?谢谢。例如:
if(a || b) {
// Do something.
}
并且
if(a | b) {
// Do something.
}
||
和|
)的区别和用法吗?谢谢。if(a || b) {
// Do something.
}
并且
if(a | b) {
// Do something.
}
5 | 6 = 7 (decimal)
101 | 110 = 111 (binary)
更多细节请查看维基百科:逻辑或
true | false
。 - Dvin|| 运算符是一个布尔运算符。
它可以简单地解释为...
if ( a is true or b is true)
{
//do soemthing
}
| 运算符是一个逻辑运算符
它只能用于像int、char等整数类型上...
它是两个操作数的按位OR运算
例如:
bool a = true;
bool b = false;
bool c = a | b;
//c will be true
if(a | b )
{
}
is same as
c = a | b;
if ( c == true)
{
do something;
}
boolean getTrue() {
System.out.println("getTrue() called");
return true;
}
public static void main(String[] args) {
boolean a = getTrue() || getTrue();
System.out.println("Result: " + a);
}