首頁  >  文章  >  Java  >  Java 布林值

Java 布林值

王林
王林原創
2024-08-30 15:11:58221瀏覽

在日常生活中,我們經常對我們的每項活動、情況、願景、結果、事件等做出決定。我們決定的價值有兩個:是或否;真或假;開或關;進行或不進行等。程式設計不屬於任何例外。在程式設計中,基於我們的核心邏輯和用例,我們需要做出決策,並基於這些決策;我們需要編寫相應的程式碼。作為一種程式語言,Java 也不例外,它允許我們提供一種稱為「布林」的特殊資料類型,以便在我們的程式碼中使用它們來進行決策。 Java 布林變數或布林運算式可以採用兩個值之一:true 或 false。

讓我們從java程式的角度討論布林值。

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

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

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

Java 布林值的型別

以下是不同類型的 Java 布林值:

Java 布林值

1. 帶有變數名稱的關鍵字布林值

關於 java 中布林類型變數的值,您只有兩個選擇。布林類型的值為 true 或 false。沒有其他可用的選擇。 不要急於尋求Java作業或任務方面的幫助;繼續閱讀以了解 Java 布林值。 您需要將關鍵字 Boolean 與變數名稱一起使用,並為其指派值(true 或 false)。

文法:

Boolean <variable_name> = <value>, where value is either true or false

例如:

boolean bool = true,其中 bool 是變數名稱,與 true 相關的值

boolean bool2 = false,其中 bool 是變數名稱,與值關聯為 false

程式碼範例 1:

public class BooleanInJava {
public static void main(String[] args){
boolean bool = true ;
boolean bool2 = false;
System.out.println(bool);
System.out.println(bool2);
}
}

輸出:

Java 布林值

2.布林類型變數

如果提供布林類型變數 true 或 false 以外的值會怎麼樣?

例如:

布林 bool = 1 ;

布爾 bool2 = 0;

您將收到一個錯誤。

程式碼範例 2:

public class BooleanInJava {
public static void main(String[] args) {
boolean bool = 1 ;
boolean bool2 = 0;
System.out.println(bool);
System.out.println(bool2);
}
}

輸出:

Java 布林值

3.布林值的特徵

現在,如何有效地利用布林的這個特性?

我們可以用它在我們的程式中做出決策。我的意思是說,您可以透過使用條件運算子來獲取或列印布林值來測試程式中的一些決定因素。這是布林表達式條件的測試。程式將評估這個表達式,並做出相應的決定。

讓我們舉一些例子:

程式碼範例 3:

public class BooleanInJava {
public static void main(String[] args) {
int num1 = 10;
int num2 =11;
System.out.println(num1 > num2); // returns false, because 11 is higher than 10
System.out.println(num2 > num1); // returns true, because 11 is higher than 10
System.out.println(num1 < num2); // returns true, because 10 is lesser than 11
System.out.println(num2 <num1); // returns false, because 10 is lesser than 11
}
}

輸出:

Java 布林值

布林值如何運作?

在本文中,我們將指出布林運算的工作原理,這意味著我們如何在程式或用例中使用布林運算的功能。由於布林值幫助我們做出決策,因此我們可以將此決策邏輯放入條件表達式中,例如:在 while 迴圈評估或 if-else 決策中。首先,讓我們看一下布林運算符,它將用於從布林表達式產生布林值,並最終使用該值進行決策。我們將在這裡使用布林邏輯運算符,它們是: | , & , ^ , ! , || , && , == , != 。讓我們使用兩個布林變數 num1 和 num2。

Symbol of Boolean operators Name of the corresponding Symbol
| OR
& AND
^ XOR
! NOT
!= NOT EQUAL
&& Short circuit AND
|| Short circuit OR
== EQUAL
布林運算子的符號 對應交易品種名稱 | 或 & 和 ^ 異或 ! 不 ! = 不等於 && 短路與 || 短路或 == 等於 表>

Please check the table for your understanding of how evaluation is happening in Boolean expressions. This understanding is very important to clear your concepts:

Variables/Boolean Expressions num1 num2 num1|num2 num1&num2 num1^num2 !num1 !num2
 

Values/Result of evaluations

true true true true false false false
true false true false true false true
false true true false true true false
false false false false false true

true

4. Public Class

Code Example 4: 

public class BooleanInJava {
public static void main(String[] args) {
boolean num1 = true;
boolean num2 = false;
System.out.println("num1|num2 = "+(num1|num2));
System.out.println("num1&num2 = "+(num1&num2));
System.out.println("num1^num2 = "+(num1^num2));
System.out.println("!num1 = "+(!num1));
System.out.println("!num2 = "+(!num2));
}
}

Output:

Java 布林值

Let us see some more code examples.

5. Boolean Operators

Code Example 5:

Here we will compare two Boolean variables and assign values to them and then create Boolean expression for those using Boolean operators and then print them to see the final output.

public class BooleanInJava {
public static void main(String[] args) {
boolean num1 = true;
boolean num2 = false;
boolean num3=(num1==num2); // Boolean expression evaluating whether values of num1 and num2 are equal or not
System.out.println(num1);
System.out.println(num2);
System.out.println(num3); //will return false as num1 and num2 have different values
}
}

Output:

Java 布林值

6. Boolean Objects.

Code Example 6:

Here we will compare two Boolean objects.

public class BooleanInJava {
public static void main(String[] args) {
boolean boolObj1=new Boolean("TRUE");
boolean boolObj2=new Boolean("FALSE");
boolean boolObj3=new Boolean("FALSE");
boolean decision=(boolObj1==boolObj2);  // evaluating values of boolObj1 and boolObj2
System.out.println("Are the value of boolean objects 1 and 2 equal? "+decision);
boolean decision2=(boolObj3==boolObj2);  // evaluating values of boolObj2 and boolObj3
System.out.println("Are the value of boolean objects 2 and 3 equal? "+decision2);
}
}

Output:

Java 布林值

Conclusion – Java Boolean

All of the comparisons and conditions in Java are primarily based on Boolean expressions; hence you need to use them in an effective manner. In this topic, you have learned about many aspects of Boolean values but, you need to use them effectively based on your business/ client requirements and use cases.

以上是Java 布林值的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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