首頁  >  文章  >  Java  >  Java 中的布林運算符

Java 中的布林運算符

WBOY
WBOY原創
2024-08-30 15:20:36501瀏覽

隨著科技的出現,電腦的發展,也帶來了對程式語言的需求。許多程式語言既包括低階語言也包括高階語言。與低階語言相比,高階語言更容易使用,因為它們更容易理解。 Java 就是這樣一種高階語言,被廣泛地用作程式設計目的的支援語言。有很多概念需要學習和練習才能理解基本概念。在本主題中,我們將討論 Java 中的布林運算子。

廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

什麼是布林運算子?

布林運算子只是一組可用於比較表達式的不同運算子。布林運算子通常有兩個值,即 false 或 true。布林運算子比較左側和右側的表達式。相比之下,它只是傳回一個布林值。

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 中的布林運算符

  • 如果一個操作數為假,另一個為真,則運算結果為假。

代碼:

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;如果所有表達式失敗,則傳回 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 中的布林運算符

  • 如果兩個運算元都為假,運算結果為假。

代碼:

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 中的布林運算符

  • 如果一個操作數為假,另一個為真,則運算結果為真。

代碼:

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.等於運算子

此運算子用於檢查運算子兩邊的運算元或運算式是否相等。

等於運算子範例
  • 如果兩個操作數不相同,則運算結果為 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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn