邏輯運算子用於對一個或兩個變數執行運算,以評估和檢索邏輯結果。通常,邏輯運算的傳回值採用布林格式,並應用於程式中以在程式的執行流程中建立更好的控制。在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中文網其他相關文章!