Java の論理演算子

王林
リリース: 2024-08-30 15:19:18
オリジナル
964 人が閲覧しました

論理演算子は、1 つまたは 2 つの変数に対して演算を実行し、論理結果を評価および取得するために使用されます。通常、論理演算の戻り値はブール形式であり、プログラムの実行フローでより適切な制御を確立するためにプログラムに適用されます。 Java で使用される論理演算子は、AND 演算の実行には「&」、OR 演算には「|」、NOT 演算には「!」、XOR 演算には「^」です。

Java の論理演算子の機能

  • 論理演算子は、実行フローの制御に使用されます。
  • ブール論理演算子は常にブール値を返します。
  • これらの演算子は 1 つ以上のブール オペランドに適用されます。
  • Java には、「&」、「|」、「!」または「~」、および「^」の 4 つの論理演算子が用意されています。

特定の入力に対する各操作の結果について、次の表を検討してみましょう。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

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 演算を実行します。この演算子は 2 つのブール値オペランドに作用し、結果はブール値になります。記号「&」または「&&」で表される AND 演算子、つまり短絡 AND 演算。

注: 単純な & 演算では、両方のオペランドの値、つまり Operand1 と Operand 2 がチェックされます。短絡 AND 演算 && では、最初の Operand1 の値がチェックされ、後でオペランド 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

FALSE (0) FALSE (0) FALSE (0)

FALSE (0)Java の論理演算子

TRUE (1) FALSE (0) TRUE (1)

FALSE (0) FALSE (0) TRUE (1)

TRUE (1) TRUE (1) テーブル> AND 演算子の例

出力:

Operand1 || Operand2
ログイン後にコピー

2.論理和演算子「|.」 Java の論理 OR 演算子は、Java で実際のデジタル OR 演算を実行するために使用されます。この演算子は 2 つのブール値オペランドとともに使用され、結果はブール値、つまり true または false になります。 Java では、論理 OR 演算子は記号「|」で表されます。 (単純な OR) または「||」 (短絡または)。

    注:
  • Java は 2 つの論理 OR 演算 (単純な OR – “|”) を使用します。および短絡 OR – 「||」。単純な論理 OR 演算では、両方のオペランド値がチェックされ、値に応じて結果が得られます。短絡 OR 演算「||」最初のオペランド、つまり Operand1 の値をチェックし、次に 2 番目のオペランド、つまり operand2 の値をチェックします。operand1 の値が 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 の場合。誰かの Operand 値が 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 の真理値表:

A B A |B

FALSE (0)

FALSE (0) FALSE (0)

FALSE (0)

TRUE (1) TRUE (1) TRUE (1) FALSE (0) TRUE (1)
!Operand or ! Condition
ログイン後にコピー
TRUE (1)

TRUE (1) TRUE (1) テーブル> OR 演算子の例

出力:
  • 3.論理否定演算子「!」または「~.」 論理 NOT 演算子は、Java で実際のデジタル NOT 演算、つまり入力値の否定を実行します。この論理演算子は単項論理演算子です。 Java では、論理 NOT 演算子は記号「!」で表されます。オペランドは 1 つだけで使用されます。または「~」の簡単な使い方!演算子は入力値を否定します。たとえば、入力が True の場合は False になります。入力が False の場合は True になります。
  • 構文:

オペランドは任意のブール値を保持します。条件は任意のブール値、つまり任意の論理演算の結果です。

A !A
FALSE (0) TRUE (1)
TRUE (1) FALSE (0)
結果: True: 入力値が False の場合、結果は True になります。 False: 入力値が True の場合、結果は False になります。 NOT の真理値表: A !A FALSE (0) TRUE (1) TRUE (1) FALSE (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 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!