首頁 > Java > java教程 > 主體

Java 中的邏輯運算符

王林
發布: 2024-08-30 15:19:18
原創
917 人瀏覽過

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

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!