Java Boolean类相关方法示例

x33g5p2x  于2021-08-19 转载在 Java  
字(4.8k)|赞(0)|评价(0)|浏览(326)

布尔类将原始类型布尔的值包装在一个对象中。一个布尔类型的对象包含一个类型为布尔的单一字段。

此外,这个类还提供了许多将布尔值转换为String的方法,以及处理布尔值时有用的其他常量和方法。

布尔类 构造函数

  • Boolean(boolean value) - 分配一个代表数值参数的布尔对象。
  • Boolean(String s) - 如果字符串参数不是空的,并且无视情况,等于字符串 "true",则分配一个布尔对象,代表值为true。
    例子:
Boolean boolean1 = new Boolean(true);
Boolean boolean2 = new Boolean("boolean");

布尔类方法

类图显示了布尔类所提供的API/方法的列表。

在这篇文章中,我们将学习Booleanwrapper类的几个重要方法。

parseBoolean(String s)

将字符串参数解析为一个布尔值。如果字符串参数不是空的,并且无视情况,等于字符串 "true",则返回的布尔值代表true。

例子:

private static void parseBoolean(){
  // 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);
}

booleanValue()

将此布尔对象的值作为一个布尔基元返回。

例子:

private static void booleanValue(){
  // 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);
}

valueOf(boolean b)

返回一个代表指定布尔值的Booleaninstance。如果指定的布尔值为真,该方法返回Boolean.TRUE;如果为假,该方法返回Boolean.FALSE。

例子:

private static void valueOf(){
 // 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);
}

valueOf(String s)

返回一个布尔值,其值由指定的字符串表示。如果字符串参数不是空的,并且无视情况,等于字符串 "true",则返回的布尔值代表一个真值。

例子:

private static void valueOf1(){
  // 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);
}

toString(boolean b)

返回一个代表指定布尔值的字符串对象。

例子:

private static void toStringTest(){
  // 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);
 
}

toString()

返回一个代表此布尔值的字符串对象。

例子:

private static void toStringTest1(){
  // 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);
}

hashCode()

返回这个布尔对象的哈希代码。

例子:

private static void hashCodeTest(){
 // 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());
}

equals(Object obj)

当且仅当参数不为空,并且是一个与此对象代表相同布尔值的布尔对象时返回真。

例子:

private static void equalsTest(){
 // 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));
}

compareTo(Boolean b)

将这个布尔实例与另一个布尔实例进行比较。

例子:

private static void comparetoTest(){
 // 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));
}

compare(boolean x, boolean y)

比较两个布尔值。返回的值与Boolean.valueOf(x).compareTo(Boolean.valueOf(y))所返回的值相同。

例子:

private static void compareTest(){
 // 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));
}

###参考

https://docs.oracle.com/javase/8/docs/api/java/lang/Boolean.html

相关文章