Home >Java >javaTutorial >The difference between || and | in java
|| and | in Java are logical operators used to connect two Boolean values. || has higher priority (logical OR), evaluates from left to right, and stops evaluation immediately if the first operand is true. Whereas | has lower precedence (bitwise OR), each operand is evaluated bitwise, and if the corresponding bit of any operand is 1, then that bit of the result is also 1.
The difference between || and | operators in Java
Brief description:
| and | in Java are both logical operators used to connect two Boolean values, but they have different priorities and evaluation rules.
Details:
Priority:
Evaluation rules:
##|| (Logical OR):
| (Bitwise OR):
Example:
|| (logical OR) :
<code>boolean result = true || false; // 结果为 true System.out.println(result);</code>In the first example, since the first operand is true, the || operator immediately evaluates to true without executing the second operand.
| (Bitwise OR):
<code>int result = 1 | 2; // 结果为 3(01 | 10 = 11) System.out.println(result);</code>In the second example, the | operator's binary representation of each operand Performs a bitwise OR operation, producing the result 11 (3 in decimal).
Conclusion: || and | operators are used to concatenate Boolean values, but have different precedence and evaluation rules. Use || when you need to logically join conditions, and | when you need to perform bitwise operations.
The above is the detailed content of The difference between || and | in java. For more information, please follow other related articles on the PHP Chinese website!