如何将String对象转换为Boolean对象。
例
在这个例子中,我们将看到String到Boolean对象转换的两种方法。
class StringObjToBooleanObj { public static void main(String[] args) { // String Objects String str = "false"; // Case does not matter for conversion String str2 = "TrUe"; /* Method 1: Using Constructor - * Passing string value to the constructor * of Boolean class. */ Boolean bobj = new Boolean(str); Boolean bobj2 = new Boolean(str2); System.out.println(bobj); System.out.println(bobj2); /* Method 2: Using valueOf() method of * Boolean class */ Boolean bobj3 = Boolean.valueOf(str); Boolean bobj4 = Boolean.valueOf(str2); System.out.println(bobj3); System.out.println(bobj4); } }输出:
falsetruefalsetrue
public static Boolean valueOf(String s):返回一个布尔值,其值由指定的字符串表示。返回的布尔值表示如果字符串参数不为null且与字符串"true"相等(忽略大小写),则返回true值。
