隨著科技的出現,電腦的發展,也帶來了對程式語言的需求。許多程式語言既包括低階語言也包括高階語言。與低階語言相比,高階語言更容易使用,因為它們更容易理解。 Java 就是這樣一種高階語言,被廣泛地用作程式設計目的的支援語言。有很多概念需要學習和練習才能理解基本概念。在本主題中,我們將討論 Java 中的布林運算子。
廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
布林運算子只是一組可用於比較表達式的不同運算子。布林運算子通常有兩個值,即 false 或 true。布林運算子比較左側和右側的表達式。相比之下,它只是傳回一個布林值。
Java中有多種類型的布林運算符。以下是 Java 中使用最廣泛的各種類型的布林運算符。
這是一個使用 && 運算子來比較邏輯運算式的邏輯賦值。如果多個邏輯中的任何一個失敗,它通常會給出 false;如果所有表達式都產生 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 } }
現在,執行上面的程式碼
輸出:
代碼:
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 } }
現在,執行上面的程式碼
輸出:
代碼:
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 } }
現在,執行上面的程式碼
輸出:
代碼:
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 } }
現在,執行上面的程式碼
輸出:
這是一個使用 || 的邏輯賦值比較邏輯表達式的運算子。通常,如果任何一個表達式為真,則傳回 true;如果所有表達式失敗,則傳回 false。
代碼:
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 } }
現在,執行上面的程式碼
輸出:
代碼:
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 } }
現在,執行上面的程式碼
輸出:
代碼:
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 } }
現在,執行上面的程式碼
輸出:
代碼:
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 } }
現在,執行上面的程式碼
輸出:
此運算子用於檢查運算子兩邊的運算元或運算式是否相等。
代碼:
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 } }
現在,執行上面的程式碼
輸出:
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:
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.
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:
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:
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 ?:
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:
In expression, (a < b) ? a : b it evaluates the value. Based on the evaluation, it executes if or else block
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中文網其他相關文章!