首页  >  文章  >  Java  >  Java 中的逻辑运算符

Java 中的逻辑运算符

王林
王林原创
2024-08-30 15:19:18962浏览

逻辑运算符用于对一个或两个变量执行运算,以评估和检索逻辑结果。通常,逻辑运算的返回值采用布尔格式,并应用于程序中以在程序的执行流程中建立更好的控制。在Java中,使用的逻辑运算符是“&”表示AND运算,“|”表示OR运算,“!”表示NOT运算,“^”表示XOR运算。

Java 中逻辑运算符的特点

  • 逻辑运算符用于控制执行流程。
  • 布尔逻辑运算符始终返回布尔值。
  • 这些运算符应用于一个或多个布尔操作数。
  • Java提供了4个逻辑运算符“&”、“|”、“!”或“~”和“^”。

让我们考虑下表,了解特定输入上每个操作的结果。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

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)

Java 中不同的逻辑运算符及说明

下表显示了运算符及其描述。

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.

1.逻辑与运算符“&”

逻辑“&”运算符执行数字与运算。该运算符作用于两个布尔操作数,结果将为布尔值。 AND 运算符用符号“&”或“&&”表示,即短路AND运算。

注意:在简单的&操作中,它检查两个操作数的值,即操作数1和操作数2。在短路AND操作&&中,它检查第一个操作数1的值,稍后它将与操作数2的值一致当且仅当操作数 1 的值为 true 时。

语法:

Operand1 & Operand2

Operand1 和 Operand2 是任意布尔值。

输出:

  1. True:当且仅当两个操作数值均为 True 时,结果为 True。
  2. False: 当任一操作数值为 false 时,结果为 False。如果两个值都是 False。

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)
A
B
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) 假(0) 假(0)

假 (0)Java 中的逻辑运算符

正确 (1) 假(0) 正确 (1)

假(0) 假(0) 正确 (1)

正确 (1) 正确 (1) 表> AND 运算符示例

输出:

Operand1 || Operand2

2.逻辑或运算符“|.” java中的逻辑OR运算符用于在java中执行实际的数字OR运算。该运算符与两个布尔操作数一起使用,结果将为布尔值,即 true 或 False。在java中,逻辑或运算符用符号“|”表示(简单或)或“||” (短路或)。

    注意:
  • Java 使用两个逻辑 OR 运算,简单的 OR – “|”和短路或 - “||”。在简单的逻辑或运算中,将检查两个操作数值,并根据值给出结果。在短路或运算“||”中它检查第一个操作数(即操作数 1)的值,然后检查第二个操作数(即操作数 2)的值,操作数 1 的值为 true 或 false。 语法:
  • Operand1 和 Operand2 是任意布尔值。

输出:

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)
True:
如果两个操作数值均为 True。假设任何操作数值为 True。
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。

Java 中的逻辑运算符

OR 真值表:

一个 B A|B

假 (0)

假(0) 假(0)

假 (0)

正确 (1) 正确 (1) 正确 (1) 假(0) 正确 (1)
!Operand or ! Condition
正确 (1)

正确 (1) 正确 (1) 表> OR 运算符示例

输出:
  • 3.逻辑非运算符“!”或“~” 逻辑非运算符在java中执行实际的数字非运算,即输入值的求反。该逻辑运算符是一元逻辑运算符;它只与一个操作数一起使用。在java中逻辑非运算符用符号“!”表示或“~”。简单使用!该运算符的作用是对输入值求反。例如,输入 True 使其为 False,或者如果输入为 False 则使其变为 True。
  • 语法:

操作数可以保存任何布尔值。条件是任何布尔值,即任何逻辑运算的结果。

A !A
FALSE (0) TRUE (1)
TRUE (1) FALSE (0)
结果: True: 如果输入值为 False,则结果为 True。 False:如果输入值为 True,则结果为 False。 非真值表: A !A 假 (0) 正确 (1) 正确 (1) 假(0) 表>
Example of NOT Operator
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:

Java 中的逻辑运算符

4. Logical XOR Operator “ ^.”

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:

  • True: The result is True if any one of the input is True.
  • False: The result is False if both the inputs are the Same.

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)
Example of XOR Operator
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:

Java 中的逻辑运算符

Conclusion

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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn