java.lang.Boolean parseBoolean(String s)

Description

The Boolean.parseBoolean(String s) java method is used primarily in parsing a String method argument into a Boolean object. The Boolean object is a wrapper class for the boolean primitive data type of java API. Even though the conversion of String to Boolean object is a basic stuff, I am suggesting that we should have a mastery on it. In my years of java programming, I have encountered so many instances that I have to convert from String object to another data type. The Boolean.parseBoolean method is a very convenient Java API to transform a user input in String format into a more robust object type which in this case the Boolean. Make a note that the parseBoolean method of Boolean class is static thus it should be accessed statically. What I mean to say is that we should use this method such as below:

Boolean.parseBoolean(method args)

Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java parseboolean method non statically.

Method Syntax

public static boolean parseBoolean(String s)

Method Argument

Data Type Parameter Description
String s the String containing the boolean representation to be parsed

Method Returns

The parseBoolean() method of Boolean class returns the boolean represented by the string argument.

Discussion

The parseBoolean method of Boolean class is one of the newest addition as of Java 1.5. This is to take into consideration the instances wherein we have to convert String value to Boolean. This method basically 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.

Java Boolean parseBoolean(String s) Example

Below is a simple java example on the usage of parseBoolean(String s) method of Boolean class.

package com.javatutorialhq.java.examples;

import java.util.Scanner;

/*
 * This example source code demonstrates the use of 
 * parseBoolean(String s) method of Boolean class.
 */

public class BooleanParseBooleanExample {

	public static void main(String[] args) {
		
		// ask for user input
		System.out.print("Are you single (true/false)?");
		
		// read the user input
		Scanner s = new Scanner(System.in);
		String value = s.nextLine();
		s.close();
		
		// convert the user input into boolean
		boolean answer = Boolean.parseBoolean(value);
		
		// evaluate the user input
		if(answer){
			System.out.println("You are Single");
		}else{
			System.out.println("You are married");
		}
		
	}

}

To fully demonstrate the behaviour of parseBoolean(String s) method, the program above is designed in such a way that user can enter two boolean values and then the result of parseBoolean(String s) method has been evaluated using if else and appropriate message were displayed on the console. On this way, the user can experiment on different values as input.

Sample Output

Below is the sample output when you run the above example.

java Boolean parseBoolean() example output