當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Java.lang.Boolean用法及代碼示例


Java在java.lang包中提供了一個包裝類Boolean。 Boolean 類將基本類型 boolean 的值包裝在對象中。布爾類型的對象包含單個字段,其類型為布爾值。此外,此類還提供了有用的方法,例如在處理布爾變量時將布爾值轉換為字符串以及將字符串轉換為布爾值。

Creating a Boolean object

Boolean 類提供了兩個構造函數來創建 Boolean 對象。

  • 下麵的語句創建一個包含 value 參數的布爾對象。
Boolean b = new Boolean(boolean value);
  • 下麵的語句創建一個布爾對象,如果字符串參數不為空且等於字符串 “true”(忽略大小寫),則該對象包含值 true,否則創建值為 false 的布爾對象。
Boolean b = new Boolean(String s);

領域:

  • 靜態布爾值 FALSE:與原始值 false 對應的布爾對象。
  • 靜態布爾值 TRUE:與原始值 true 對應的布爾對象。
  • 靜態類:表示基本類型布爾值的 Class 對象。

方法:

  • 靜態布爾 parseBoolean(String s):此方法將字符串參數解析為布爾值。如果字符串參數不為空且等於字符串 “true”(忽略大小寫),則返回的布爾值表示值 true,否則返回 false。
Syntax : 
public static boolean parseBoolean(String s)
Parameters : 
s - the String containing the boolean representation to be parsed
返回:
the boolean represented by the string argument

Java


// Java program to demonstrate parseBoolean() method
public class Test
{
    public static void main(String[] args)
    {
        // parsing different Strings
        boolean b1 = Boolean.parseBoolean("True");
        boolean b2 = Boolean.parseBoolean("TruE");
        boolean b3 = Boolean.parseBoolean("False");
        boolean b4 = Boolean.parseBoolean("FALSE");
        boolean b5 = Boolean.parseBoolean("GeeksForGeeks");
         
        System.out.println(b1);
        System.out.println(b2);
        System.out.println(b3);
        System.out.println(b4);
        System.out.println(b5);
         
    }
}

輸出:

true
true
false
false
false
  • 布爾值booleanValue():此方法返回此布爾對象的值作為布爾基元。
Syntax : 
public boolean booleanValue()
Parameters : 
NA
返回:
the primitive boolean value of this object.

Java


// Java program to demonstrate booleanValue() method
public class Test
{
    public static void main(String[] args)
    {
        // creating different Boolean objects
        Boolean b1 = new Boolean("True");
        Boolean b2 = new Boolean("False");
        Boolean b3 = new Boolean("GeeksForGeeks");
         
        // getting primitive boolean value
        boolean b4 = b1.booleanValue();
        boolean b5 = b2.booleanValue();
        boolean b6 = b3.booleanValue();
         
        System.out.println(b4);
        System.out.println(b5);
        System.out.println(b6);
         
    }
}

輸出:

true
false
false
  • 靜態布爾 valueOf(布爾 b):此方法返回表示指定布爾值的布爾實例。如果指定的布爾值為 true,則返回 Boolean.TRUE,如果為 false,則此方法返回 Boolean.FALSE。接下來討論該方法的另一種變體。
Syntax : 
public static boolean valueOf(boolean b)
Parameters : 
b - a boolean value.
返回:
a Boolean object representing b.

Java


// Java program to demonstrate valueOf() method
public class Test
{
    public static void main(String[] args)
    {
        // creating boolean variable
        boolean b1 = true;
        boolean b2 = false;
         
        // getting Boolean objects from boolean variables
        Boolean b3 = Boolean.valueOf(b1);
        Boolean b4 = Boolean.valueOf(b2);
         
        System.out.println(b3);
        System.out.println(b4);
         
    }
}

輸出:

true
false
  • 靜態布爾值(String s):此方法返回一個布爾值,其值由指定字符串‘s’表示。如果字符串參數不為空且等於字符串“true”(忽略大小寫),則返回的布爾值表示真值。
Syntax : 
public static boolean valueOf(String s)
Parameters : 
s - a string
返回:
a Boolean value represented by the string

Java


// Java program to demonstrate valueOf() method
public class Test
{
    public static void main(String[] args)
    {
        // creating boolean variable using different Strings
        Boolean b1 = Boolean.valueOf("true");
        Boolean b2 = Boolean.valueOf("TRue");
        Boolean b3 = Boolean.valueOf("False");
        Boolean b4 = Boolean.valueOf("GeeksForGeeks");
        Boolean b5 = Boolean.valueOf(null);
         
        System.out.println(b1);
        System.out.println(b2);
        System.out.println(b3);
        System.out.println(b4);
        System.out.println(b5);
         
    }
}

輸出:

true
true
false
false
false
  • 靜態字符串 toString(布爾 b):此方法返回表示指定布爾值的 String 對象。如果指定的布爾值為 true,則將返回字符串“true”,否則將返回字符串“false”。接下來討論此方法的其他變體。
Syntax : 
public static String toString(boolean b)
Parameters : 
b - the boolean to be converted
返回:
the string representation of the specified boolean

Java


// Java program to demonstrate toString() method
public class Test
{
    public static void main(String[] args)
    {
        // creating boolean variable
        boolean b1 = true;
        boolean b2 = false;
         
        // getting String value of the primitives boolean
        String str1 = Boolean.toString(b1);
        String str2 = Boolean.toString(b2);
         
        System.out.println(str1);
        System.out.println(str2);
    }
}

輸出:

true
false
  • 字符串toString():此方法返回表示此布爾值的 String 對象。如果此對象表示值 true,則返回等於 “true” 的字符串。否則,返回字符串“false”。
Syntax : 
public String toString()
Parameters : 
NA
返回:
a string representation of this object
Overrides :
toString in class Object

Java


// Java program to demonstrate toString() method
public class Test
{
    public static void main(String[] args)
    {
        // creating different Boolean objects
        Boolean b1 = new Boolean("True");
        Boolean b2 = new Boolean("False");
        Boolean b3 = new Boolean("GeeksForGeeks");
        Boolean b4 = new Boolean(null);
     
         
        // getting String value of Boolean objects
        String str1 = b1.toString();
        String str2 = b2.toString();
        String str3 = b3.toString();
        String str4 = b4.toString();
         
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
        System.out.println(str4);
    }
}

輸出:

true
false
false
false
  • int hashCode():此方法返回此布爾對象的哈希碼。請注意,true 的哈希碼為 1231,false 的哈希碼為 1237。要查找選擇此整數作為哈希碼的原因,請參閱這裏.
Syntax : 
public int hashCode()
Parameters : 
NA
返回:
the integer 1231 if this object represents true;
returns the integer 1237 if this object represents false
Overrides :
hashCode in class Object

Java


// Java program to demonstrate hashCode() method
public class Test
{
    public static void main(String[] args)
    {
        // creating different Boolean objects
        Boolean b1 = new Boolean("True");
        Boolean b2 = new Boolean("False");
        Boolean b3 = new Boolean("TRue");
        Boolean b4 = new Boolean(null);
     
        System.out.println(b1.hashCode());
        System.out.println(b2.hashCode());
        System.out.println(b3.hashCode());
        System.out.println(b4.hashCode());
    }
}

輸出:

1231
1237
1231
1237
  • 布爾等於(對象 obj):如果參數不為 null 並且是表示與此對象相同的布爾值的布爾對象,則此方法返回 true。
Syntax : 
public boolean equals(Object obj)
Parameters : 
obj - the object to compare with.
返回:
true if the Boolean objects represent the same value; 
false otherwise
Overrides :
equals in class Object

Java


// Java program to demonstrate equals() method
public class Test
{
    public static void main(String[] args)
    {
        // creating different Boolean objects
        Boolean b1 = new Boolean("True");
        Boolean b2 = new Boolean("False");
        Boolean b3 = new Boolean("TrUe");
        Boolean b4 = new Boolean(null);
         
     
        // checking equality of Boolean objects
        System.out.println(b1.equals(b2));
        System.out.println(b2.equals(b4));
        System.out.println(b1.equals(b3));
        System.out.println(b1.equals(b4));
    }
}

輸出:

false
true
true
false
  • intcompareTo(布爾 b):此方法“compares”帶有傳遞參數‘b’的布爾實例。
Syntax : 
public int compareTo(Boolean b)
Parameters : 
b - the Boolean instance to be compared
返回:
zero if this object represents the same boolean value as the argument; 
a positive value if this object represents true and the argument represents false;
a negative value if this object represents false and the argument represents true.
Throws :
NullPointerException - if the argument is null

Java


// Java program to demonstrate compareTo() method
public class Test
{
    public static void main(String[] args)
    {
        // creating different Boolean objects
        Boolean b1 = new Boolean("True");
        Boolean b2 = new Boolean("False");
        Boolean b3 = new Boolean("TRue");
        Boolean b4 = new Boolean(null);
     
        //comparing b1,b2,b3,b4
        System.out.println(b1.compareTo(b2));
        System.out.println(b1.compareTo(b3));
        System.out.println(b2.compareTo(b1));
        System.out.println(b1.compareTo(b4));
        System.out.println(b2.compareTo(b4));
         
        // The following statement throws NullPointerExcetion
        //  System.out.println(b1.compareTo(null));
    }
}

輸出:

1
0
-1
1
0
  • int 比較(布爾值 x,布爾值 y):此方法用於“compares”原語布爾變量。
Syntax : 
public static int compare(boolean x, boolean y)
Parameters : 
x - the first boolean to compare
y - the second boolean to compare
返回:
zero if x is same boolean value as y; 
a positive value x is true and y is false;
a negative value if x is false and y is true;
Throws :
NullPointerException - if the argument is null

Java


// Java program to demonstrate compare() method
public class Test
{
    public static void main(String[] args)
    {
        // creating boolean variable
        boolean b1 = true;
        boolean b2 = false;
        boolean b3 = true;
        boolean b4 = false;
         
        //comparing b1,b2,b3,b4
        System.out.println(Boolean.compare(b1, b2));
        System.out.println(Boolean.compare(b1, b3));
        System.out.println(Boolean.compare(b2, b1));
        System.out.println(Boolean.compare(b2, b4));
         
        // The following statement throws NullPointerExcetion
        //  System.out.println(Boolean.compare(b1, null));
    }
}

輸出:

1
0
-1
0


相關用法


注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Java.lang.Boolean Class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。