Description
How to convert String object to boolean primitive in Java.
Example: Complete conversion code
In this example we are converting string to boolean using parseBoolean() method of Boolean class.
class StringToboolean { public static void main(String[] args) { // String Objects String str = "false"; // Case does not matter for conversion String str2 = "TrUe"; // String to boolean conversion boolean bvar = Boolean.parseBoolean(str); boolean bvar2 = Boolean.parseBoolean(str2); // Displaying boolean values System.out.println("boolean value of String str is: "+bvar); System.out.println("boolean value of String str2 is: "+bvar2); } }
Output:
boolean value of String str is: false boolean value of String str2 is: true
public static boolean parseBoolean(String s)
: Parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string “true”.
Example: Boolean.parseBoolean(“True”) returns true.
Example: Boolean.parseBoolean(“yes”) returns false.
Source: parseBoolean() method – Boolean javadoc
Leave a Reply