我见过在Java程序的方法调用中使用的竖线字符。
例如:
public class Testing1 {
public int print(int i1, int i2){
return i1 + i2;
}
public static void main(String[] args){
Testing1 t1 = new Testing1();
int t3 = t1.print(4, 3 | 2);
System.out.println(t3);
}
}当我运行这段代码时,我只是得到了7。
有人能解释一下管道在方法调用中的作用以及如何正确使用它吗?
发布于 2013-05-08 22:16:29
3 | 2中的管道是bitwise inclusive OR操作符,在本例中它返回3(二进制形式为11 | 10 == 11)。
发布于 2014-02-03 05:13:02
|运算符对操作数执行按位OR操作:
3 | 2 ---> 0011 (3 in binary)
OR 0010 (2 in binary)
---------
0011 (3 in binary)模式是这样的:
0 OR 0: 0
0 OR 1: 1
1 OR 0: 1
1 OR 1: 1使用|
if(someCondition | anotherCondition)
{
/* this will execute as long as at least one
condition is true */
}请注意,这类似于if语句中常用的short-circuit OR (||):
if(someCondition || anotherCondition)
{
/* this will also execute as long as at least one
condition is true */
}(除了||不强制要求在找到真表达式后继续检查其他条件。)
https://stackoverflow.com/questions/16443028
复制相似问题