What is the Boolean.parseBoolean method in Java?

The static method parseBoolean of the Boolean class can be used to parse the Boolean value from a string.

Syntax


public static boolean parseBoolean(String s)

Returns

This method returns true if the content of the string passed is equal to true, which ignores the case of the string. False will be returned for any other value.

parseBoolean(s)

Argument

Return Value

"true"

true

"TRUe"

true

"truE"

true

"1"

false

Code

class ParseBooleanTest {
public static void main( String args[] ) {
System.out.println( "Boolean.parseBoolean('true') : " + Boolean.parseBoolean("true"));
System.out.println( "Boolean.parseBoolean('TRUE') : " + Boolean.parseBoolean("TRUE"));
System.out.println( "Boolean.parseBoolean('truE') : " + Boolean.parseBoolean("truE"));
System.out.println( "Boolean.parseBoolean('TruE') : " + Boolean.parseBoolean("TruE"));
System.out.println( "Boolean.parseBoolean(null) : " + Boolean.parseBoolean(null));
System.out.println( "Boolean.parseBoolean('1') : " + Boolean.parseBoolean("1"));
}
}

Explanation

In the code above, we used the parseBoolean method to convert the string into a Boolean value. We got true as the return value for all the strings whose content is equal to true by ignoring the case. We got false for all other strings.

Free Resources