Description
How to convert String object to Boolean object.
Example
In this example we are gonna see two methods of String to Boolean object conversion.
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); } }
Output:
false true false true
public static Boolean valueOf(String s)
: Returns a Boolean with a value represented by the specified string. The Boolean returned represents a true value if the string argument is not null and is equal, ignoring case, to the string “true”.
Source: valueOf(String s) method – Boolean Javadoc
Leave a Reply