Java のブール演算子

WBOY
リリース: 2024-08-30 15:20:36
オリジナル
499 人が閲覧しました

テクノロジーの出現によりコンピューターが発展し、それに伴いプログラミング言語が必要になりました。多くのプログラミング言語には、低級言語と高級言語の両方が含まれています。高級言語は低級言語に比べて理解しやすいため、使いやすくなります。 Java はそのような高級言語の 1 つであり、プログラミング目的のバックグラウンド言語として広く使用されています。基本的な概念を理解するために勉強し、練習する必要がある概念がたくさんあります。このトピックでは、Java のブール演算子について説明します。

広告 このカテゴリーの人気コース JAVA マスタリー - スペシャライゼーション | 78 コース シリーズ | 15 回の模擬テスト

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

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

ブール演算子とは何ですか?

ブール演算子は、式を比較するために使用できるさまざまな演算子のセットにすぎません。ブール演算子には通常、false または true の 2 つの値があります。ブール演算子は、左側と右側の式を比較します。それに比べて、これは単にブール値を返します。

Java のブール演算子の種類

Java にはさまざまな種類のブール演算子があります。以下は、Java で最も広く使用されているさまざまなタイプのブール演算子です。

Java のブール演算子

1.論理積演算子

これは、&& 演算子を使用して論理式を比較する論理代入です。通常、複数のロジックのいずれかが失敗した場合は false が返され、すべての式が true を生成した場合は true が返されます。

AND 演算子の例
  • 両方のオペランドが true の場合、演算結果は true になります

コード:

public class Main
{
public static void main(String[] args) {
boolean a = true;
boolean b = true;
System.out.println (a && b); // shows the logical operation using operator
}
}
ログイン後にコピー

次に、上記のコードを実行します

出力:

Java のブール演算子

  • 両方のオペランドが false の場合、演算結果は false になります。

コード:

public class Main
{
public static void main(String[] args) {
boolean a = false;
boolean b = false;
System.out.println (a && b); // shows the logical operation using operator
}
}
ログイン後にコピー

次に、上記のコードを実行します

出力:

Java のブール演算子

  • 一方のオペランドが true で、もう一方のオペランドが false の場合、演算結果は false になります。

コード:

public class Main
{
public static void main(String[] args) {
boolean a = true;
boolean b = false;
System.out.println (a && b); // shows the logical operation using operator
}
}
ログイン後にコピー

次に、上記のコードを実行します

出力:

Java のブール演算子

  • 一方のオペランドが false で、もう一方のオペランドが true の場合、演算結果は false になります。

コード:

public class Main
{
public static void main(String[] args) {
boolean a = false;
boolean b = true;
System.out.println (a && b); // shows the logical operation using operator
}
}
ログイン後にコピー

次に、上記のコードを実行します

出力:

Java のブール演算子

2.論理和演算子

これは || を使用する論理代入です。論理式を比較する演算子。通常、いずれかの式が true になった場合は true を返し、すべての式が失敗した場合は false を返します。

OR 演算子の例
  • 両方のオペランドが true の場合、演算結果は true になります。

コード:

public class Main
{
public static void main(String[] args) {
boolean a = true;
boolean b = true;
System.out.println (a || b); // shows the logical operation using operator
}
}
ログイン後にコピー

次に、上記のコードを実行します

出力:

Java のブール演算子

  • 両方のオペランドが false の場合、演算結果は false になります。

コード:

public class Main
{
public static void main(String[] args) {
boolean a = false;
boolean b = false;
System.out.println (a || b); // shows the logical operation using operator
}
}
ログイン後にコピー

次に、上記のコードを実行します

出力:

Java のブール演算子

  • 一方のオペランドが true で、もう一方のオペランドが false の場合、演算結果は true になります。

コード:

public class Main
{
public static void main(String[] args) {
boolean a = true;
boolean b = false;
System.out.println (a || b); // shows the logical operation using operator
}
}
ログイン後にコピー

次に、上記のコードを実行します

出力:

Java のブール演算子

  • 一方のオペランドが false で、もう一方のオペランドが true の場合、演算結果は true になります。

コード:

public class Main
{
public static void main(String[] args) {
boolean a = false;
boolean b = true;
System.out.println (a || b); // shows the logical operation using operator
}
}
ログイン後にコピー

次に、上記のコードを実行します

出力:

Java のブール演算子

3.演算子と等しい

この演算子は、演算子の両側のオペランドまたは式が等しいかどうかを確認するために使用されます。

Equal to 演算子の例
  • 両方のオペランドが同じでない場合、演算結果は false になります。

コード:

public class Main
{
public static void main(String[] args) {
String a = "abc";
String b = "abcd";
System.out.println (a == b); // shows the logical operation using operator
}
}
ログイン後にコピー

次に、上記のコードを実行します

出力:

Java のブール演算子

  • If both operands are the same, the operation result is true.

Code:

public class Main
{
public static void main(String[] args) {
String a = "abc";
String b = "abc";
System.out.println (a == b); // shows the logical operation using operator
}
}
ログイン後にコピー

Now, execute the above code

Output:

Java のブール演算子

4. Not Equal to Operator

This operator is used to check if operand or expression on both sides of the operator are equal or not. It produces true if operands on both sides are not the same; else gives false.

Examples of not equal to operator
  • If both operands are not the same, the operation result is true.

Code:

public class Main
{
public static void main(String[] args) {
String a = "abc";
String b = "abcd";
System.out.println (a != b); // shows the logical operation using operator
}
}
ログイン後にコピー

Now, execute the above code

Output:

Java のブール演算子

  • If both operands are the same, the operation result is false.

Code:

public class Main
{
public static void main(String[] args) {
String a = "abc";
String b = "abc";
System.out.println (a != b); // shows the logical operation using operator
}
}
ログイン後にコピー

Now, execute the above code

Output:

Java のブール演算子

5. Ternary Operator

This operator is used to check if else conditions. It is generally shorthand for the if-else statement. If the expression is true, then if the part is executed otherwise, else block is executed. It uses two operands which are ?:

Example of Ternary Operator
public class Main
{
public static void main (String[]args){
int a = 2;
int b = 5;
int minOfNum = (a < b) ? a : b;
System.out.println (minOfNum);
}
}
ログイン後にコピー

Output:

Java のブール演算子

In expression, (a < b) ? a : b it evaluates the value. Based on the evaluation, it executes if or else block

Conclusion

Java is a programming language where there are lots of concepts that one needs to study. Boolean operators are one of those. These boolean operators basically execute the code to check whether the expression value is true or not. Based on the expression evaluation, it returns the value. A boolean operator is widely used in any programming language to various logical programming expressions.

以上がJava のブール演算子の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

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