java OR 运算符 || 之间的区别 和| 在爪哇?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10084524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between OR operator || and | in Java?
提问by AKZap
Possible Duplicate:
Why do we usually use||
not|
, what is the difference?
可能的重复:
为什么我们通常使用||
not|
,有什么区别?
Can any one explain the difference between and usage of OR operator ( ||
and |
) in java. thanks
任何人都可以解释Java中OR运算符(||
和|
)的区别和用法。谢谢
e.g:
例如:
if(a || b) {
// Do something.
}
and
和
if(a | b) {
// Do something.
}
回答by Sam
This is simple. http://www.roseindia.net/help/java/o/java-operator.shtmlsays:
这很简单。http://www.roseindia.net/help/java/o/java-operator.shtml说:
OR operator is a kind of a conditional operators, which is represented by | symbol. It returns either true or false value based on the state of the variables i.e. the operations using conditional operators are performed between the two boolean expressions.
The OR operator (|) is similar to the Conditional-OR operator (||) and returns true, if one or another of its operand is true.
OR运算符是一种条件运算符,用|表示 象征。它根据变量的状态返回真或假值,即在两个布尔表达式之间执行使用条件运算符的操作。
OR 运算符 (|) 类似于条件或运算符 (||),如果其操作数中的一个或另一个为真,则返回真。
Note: In ||
operator if have more than one condition and if first condition return true
then other conditions ignored but in |
operator all condition examin.
注意:在||
运算符中,如果有多个条件,如果第一个条件,return true
则其他条件将被忽略,但在|
运算符中,所有条件都会检查。
There are more information on http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html上有更多信息
回答by Nitram
The first one is a logical or. Both sides of the operator are handled as boolean
values and it results in a boolean
. In case the variables in question are not boolean
themselves they become false if they are 0
or null
.
第一个是逻辑或。运算符的两边都作为boolean
值处理,结果是boolean
. 如果所讨论的变量不是boolean
它们本身,如果它们是0
或 ,它们就会变成假null
。
The second one is a bit-wise or operator. This one usually only works with integer numbers. I compares the two values bit by bit and gives the resulting number. For example:
第二个是按位或运算符。这个通常只适用于整数。我逐位比较这两个值并给出结果数字。例如:
5 | 6 = 7 (decimal)
101 | 110 = 111 (binary)
For further details have a look at Wikipedia: Logical disjunction
有关更多详细信息,请查看维基百科:逻辑析取
回答by Michael
If you use the operator || JVM will not bother to evaluate the right-hand operand alone.
如果您使用运算符 || JVM 不会费心单独评估右手操作数。
回答by sgarizvi
the || operator is a boolean operator
|| 运算符是一个布尔运算符
it can be interpreted in simple english as...
它可以用简单的英语解释为...
if ( a is true or b is true)
{
//do soemthing
}
the | operator is a logical operator
| 运算符是逻辑运算符
it works only on integral types like int, char etc...
它只适用于整数类型,如 int、char 等......
it is the bitwise OR operation on the two operands
它是两个操作数的按位或运算
example:
例子:
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;
}
回答by Jaco Van Niekerk
The first is a logical-or, the latter a bitwise-or. HOWEVER, if the two operators (a and b in your example), are boolean, the bitwise-or is seen as logical-or without short circuiting. This can be be convenient at times.
第一个是逻辑或,后者是按位或。但是,如果两个运算符(在您的示例中为 a 和 b)是布尔值,则按位或被视为逻辑或没有短路。这有时会很方便。
For example, consider:
例如,考虑:
boolean getTrue() {
System.out.println("getTrue() called");
return true;
}
public static void main(String[] args) {
boolean a = getTrue() || getTrue();
System.out.println("Result: " + a);
}
The above will only print "getTrue() called" once as the logical-or (||) can determine the result of the expression immediately, without calling getTrue() a second time. Changing to a bitwise-or (i.e. boolean a = getTrue() | getTrue();) will result in two calls to getTrue().
上面只会打印一次“调用 getTrue()”,因为逻辑或 (||) 可以立即确定表达式的结果,而无需第二次调用 getTrue()。更改为按位或(即 boolean a = getTrue() | getTrue();)将导致对 getTrue() 的两次调用。
A similar result will be produced with a bitwise-& operation and a getFalse() method.
使用按位 & 操作和 getFalse() 方法将产生类似的结果。
Another aspect to keep into consideration is that the bit-wise operators gets preference before logical operators. Therefore, mixing them is not recommended as bitwise-or will be executed before a logical-and, which can cause unwanted behaviour. It can be fixed using brackets (), but I think this should be avoided.
另一个需要考虑的方面是按位运算符优先于逻辑运算符。因此,不建议将它们混合,因为按位或将在逻辑与之前执行,这可能会导致不需要的行为。它可以使用括号 () 来修复,但我认为应该避免这种情况。
As a side note, I don't agree with the down-vote of your question, it is a valid and good question! (giving it a +1)
作为旁注,我不同意您的问题的反对票,这是一个有效且好的问题!(给它+1)