JAVAJava中的lang.Boolean类

Java提供了一个包装类 布尔值 在爪哇。朗包。Boolean类将布尔基元类型的值包装在对象中。布尔类型的对象包含一个类型为布尔的字段。

null

此外,该类还提供了一些有用的方法,例如在处理布尔变量时,将布尔值转换为字符串,将字符串转换为布尔值。

创建布尔对象

布尔类提供 用于创建布尔对象的构造函数。

  • 下面的语句创建一个包含value参数的布尔对象。
    Boolean b = new Boolean(boolean value);
    
  • 下面的语句创建一个布尔对象,如果字符串参数不为null,并且忽略大小写,等于字符串“true”,则包含值true,否则将创建值为false的布尔对象。
    Boolean b = new Boolean(String s);
    

领域:

  • 静态布尔值错误: 与基元值对应的布尔对象为false。
  • 静态布尔真值: 与基元值true对应的布尔对象。
  • 静态类: 表示布尔基元类型的类对象。

方法:

  1. 静态布尔解析布尔(字符串s) :此方法将字符串参数解析为布尔值。如果字符串参数不为null,并且忽略大小写与字符串“true”相等,则返回的布尔值表示值true,否则返回false。
    Syntax : 
    public static boolean parseBoolean(String s)
    Parameters : 
    s - the String containing the boolean representation to be parsed
    Returns :
    the boolean represented by the string argument
    

    // 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
    
  2. 布尔布尔值() :此方法将此布尔对象的值作为布尔基元返回。
    Syntax : 
    public boolean booleanValue()
    Parameters : 
    NA
    Returns :
    the primitive boolean value of this object.
    

    // 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
    
  3. 静态布尔值(布尔b) :此方法返回表示指定布尔值的布尔实例。如果指定的布尔值为true,则返回布尔值。如果为TRUE或false,则此方法返回布尔值。错误的接下来将讨论该方法的另一种变体。
    Syntax : 
    public static boolean valueOf(boolean b)
    Parameters : 
    b - a boolean value.
    Returns :
    a Boolean object representing b.
    

    // 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
    
  4. 静态布尔值(字符串s) :此方法返回一个布尔值,其值由指定的字符串“s”表示。如果字符串参数不为null,并且忽略大小写,与字符串“true”相等,则返回的布尔值表示真值。
    Syntax : 
    public static boolean valueOf(String s)
    Parameters : 
    s - a string
    Returns :
    a Boolean value represented by the string
    

    // 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
    
  5. 静态字符串到字符串(布尔b) :此方法返回表示指定布尔值的字符串对象。如果指定的布尔值为true,则返回字符串“true”,否则返回字符串“false”。接下来将讨论该方法的另一种变体。
    Syntax : 
    public static String toString(boolean b)
    Parameters : 
    b - the boolean to be converted
    Returns :
    the string representation of the specified boolean
    

    // 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
    
  6. String to字符串() :此方法返回表示此布尔值的字符串对象。如果此对象表示值true,则返回一个等于“true”的字符串。否则,返回字符串“false”。
    Syntax : 
    public String toString()
    Parameters : 
    NA
    Returns :
    a string representation of this object
    Overrides :
    toString in class Object
    

    // 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
    
  7. int hashCode() :此方法返回此布尔对象的哈希代码。请注意,true的哈希代码是1231,false的哈希代码是1237。要找到选择此整数作为哈希代码的原因,请参阅 在这里 .
    Syntax : 
    public int hashCode()
    Parameters : 
    NA
    Returns :
    the integer 1231 if this object represents true;
    returns the integer 1237 if this object represents false
    Overrides :
    hashCode in class Object
    

    // 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
    
  8. 布尔等于(对象obj) :如果参数不为null,并且是表示与此对象相同的布尔值的布尔对象,则此方法返回true。
    Syntax : 
    public boolean equals(Object obj)
    Parameters : 
    obj - the object to compare with.
    Returns :
    true if the Boolean objects represent the same value; 
    false otherwise
    Overrides :
    equals in class Object
    

    // 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
    
  9. int compareTo(布尔b) :此方法将此布尔实例与传递的参数“b”进行“比较”。
    Syntax : 
    public int compareTo(Boolean b)
    Parameters : 
    b - the Boolean instance to be compared
    Returns :
    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 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
    
  10. 整数比较(布尔x,布尔y) :此方法用于“比较”基本体布尔变量。
    Syntax : 
    public static int compare(boolean x, boolean y)
    Parameters : 
    x - the first boolean to compare
    y - the second boolean to compare
    Returns :
    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 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
    

本文由 高拉夫·米格拉尼 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享