逻辑运算符用于对一个或两个变量执行运算,以评估和检索逻辑结果。通常,逻辑运算的返回值采用布尔格式,并应用于程序中以在程序的执行流程中建立更好的控制。在Java中,使用的逻辑运算符是“&”表示AND运算,“|”表示OR运算,“!”表示NOT运算,“^”表示XOR运算。
让我们考虑下表,了解特定输入上每个操作的结果。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
A | B | A&B | A|B | A^B | !A or ~A |
True (1) | True(1) | True (1) | True(1) | False (0) | False (0) |
True(1) | False(0) | False(0) | True(1) | True(1) | False(0) |
False(0) | True(1) | False(0) | True(1) | True(1) | True(1) |
False(0) | False(0) | False(0) | False(0) | False(0) | True(1) |
下表显示了运算符及其描述。
Operator | Meaning | Description |
& | Logical AND | If both the inputs are True, then the result is True; if anyone input is False, the result will be False. |
| | Logical OR | The result is True if any of the input is True. The result will be false if both the inputs are False. |
! or ~ | Logical NOT | Logical NOT is a unary operator; it operates only on a single operand. It always outputs the negation of input value. |
^ | Logical XOR | The result is True if any one of the input is True. The result will be false if both the inputs are the Same. |
逻辑“&”运算符执行数字与运算。该运算符作用于两个布尔操作数,结果将为布尔值。 AND 运算符用符号“&”或“&&”表示,即短路AND运算。
注意:在简单的&操作中,它检查两个操作数的值,即操作数1和操作数2。在短路AND操作&&中,它检查第一个操作数1的值,稍后它将与操作数2的值一致当且仅当操作数 1 的值为 true 时。语法:
Operand1 & Operand2
Operand1 和 Operand2 是任意布尔值。
输出:
AND 真值表:
A | B | A & B |
FALSE (0) | FALSE (0) | FALSE (0) |
FALSE (0) | TRUE (1) | FALSE (0) |
TRUE (1) | FALSE (0) | FALSE (0) |
TRUE (1) | TRUE (1) | TRUE (1) |
package com.java.demo; public class DemoAND { public static void main(String[] args) { boolean a=true; boolean b=false; int num1=0; int num2=1; boolean out1=(a & a); boolean out2=(a & b); boolean out3=(b & a); boolean out4=(b & b); System.out.println("True & True :"+out1); System.out.println("True & False :"+out2); System.out.println("False & True :"+out3); System.out.println("False & False :"+out4); if(num1 ==0 & num2 == 1) { System.out.println("The Condition is True...."); } } }
A 和 B
假 (0)
输出:
Operand1 || Operand2
2.逻辑或运算符“|.” java中的逻辑OR运算符用于在java中执行实际的数字OR运算。该运算符与两个布尔操作数一起使用,结果将为布尔值,即 true 或 False。在java中,逻辑或运算符用符号“|”表示(简单或)或“||” (短路或)。
输出:
A | B | A |B |
FALSE (0) | FALSE (0) | FALSE (0) |
FALSE (0) | TRUE (1) | TRUE (1) |
TRUE (1) | FALSE (0) | TRUE (1) |
TRUE (1) | TRUE (1) | TRUE (1) |
package com.java.demo; public class DemoOR { public static void main(String[] args) { boolean a=true; boolean b=false; int num=0; boolean out1=(a | a); boolean out2=(a | b); boolean out3=(b | a); boolean out4=(b | b); System.out.println("True | True :"+out1); System.out.println("True | False :"+out2); System.out.println("False | True :"+out3); System.out.println("False | False :"+out4); if(num == 0 | num == 1) { System.out.println("The Number is binary.."); } } }
False: 如果两个操作数值均为 False。
OR 真值表:
假 (0)
假 (0)
!Operand or ! Condition正确 (1)
输出:
操作数可以保存任何布尔值。条件是任何布尔值,即任何逻辑运算的结果。
A | !A |
FALSE (0) | TRUE (1) |
TRUE (1) | FALSE (0) |
public class DemoNOT { public static void main(String[] args) { boolean a=true; boolean b=false; int num1=0; int num2=1; boolean out1=(a ^ a); boolean out2=(a ^ b); boolean out3=(b ^ a); boolean out4=(!b ^ b); System.out.println("True ^ True :"+out1); System.out.println("True ^ False :"+out2); System.out.println("False ^ True :"+out3); System.out.println(!b+" ^ False :"+out4); System.out.println("a=true !a="+!a); if(!(num1 ==0) ^ (num2 == 1)) { System.<em>out</em>.println("The Condition is True...."); } } }
Output:
Logical XOR operator is a short form of Exclusive OR operator. This logical operator is when we have to check or compare the values of anyone operand is True then the output is true. In Java, Logical XOR is represented by the symbol “ ^ ”.This operator is Binary Logical Operator, i.e. can be used with two operands/conditions. Output this operator is also a Boolean value.
Syntax:
Operand1 ^ Operand2 or Condition1 ^ Condition2
Operand1 and Operand2 hold any Boolean values. Condition1 and Condition2 hold any Boolean values, i.e. output any logical operation.
Output:
Truth Table of XOR:
A | B | A ^ B |
FALSE (0) | FALSE (0) | FALSE (0) |
FALSE (0) | TRUE (1) | TRUE (1) |
TRUE (1) | FALSE (0) | TRUE (1) |
TRUE (1) | TRUE (1) | FALSE (0) |
public class DemoXOR { public static void main(String[] args) { boolean a=true; boolean b=false; int num1=0; int num2=1; int num3=5; int num4=7; boolean out1=(a ^ a); boolean out2=(a ^ b); boolean out3=(b ^ a); boolean out4=(b ^ b); System.out.println("True ^ True :"+out1); System.out.println("True ^ False :"+out2); System.out.println("False ^ True :"+out3); System.out.println("False ^ False :"+out4); System.out.println("5 ^ 7 ="+(num3 ^ num4)); System.out.println("0101 ^ 0111=0010"); if((num1 ==2) ^ (num2 == 1)) { System.out.println("The Condition is True...."); } } }
Output:
It makes java code more powerful and flexible. Use logical operators in conditional statements or looping statements to look very clean. The most important benefit of the logical operator is it reduces the code complexity. For example, it reduces the number of if…else conditional statements. This indirectly benefits in code compilation, run time etc.…overall code performance is increased.
以上是Java 中的逻辑运算符的详细内容。更多信息请关注PHP中文网其他相关文章!