Learnitweb

Java program to convert String to boolean

java.lang.Boolean class provides two methods to convert String to boolean.

public static boolean parseBoolean(String s)This method 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”.
public static Boolean valueOf(String s)This method 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”.

Important points

  • Both valueOf() and parseBoolean() methods are null safe. That is, both methods will return false if you pass null i.e. parseBoolean(null) and valueOf(null) will return false instead of throwing NullPointerException.
  • Boolean.parseBoolean(String s) method returns primitive boolean whereas Boolean.valueOf(String s) returns Boolean object.
  • Both methods return false when string other than “true” and “false” is provided.
  • Both methods ignore case for passed string argument.
  • Constructor of java.lang.Boolean class can be used to to convert String to a Boolean object but it is not encouraged.
public class StringToBooleanExample {
	public static void main(String args[]) {
		String value1 = "true";
		String value2 = "TRUE";
		String value3 = "false";
		String value4 = "FALSE";
		String value5 = "hello";
		String value6 = null;
		
		//both method return true
		Boolean booleanValue1 = Boolean.parseBoolean(value1);
		Boolean booleanValue2 = Boolean.valueOf(value1);
		
		System.out.println("booleanValue1: "+booleanValue1);
		System.out.println("booleanValue2: "+booleanValue2);
		
		//Both methods return true
		Boolean booleanValue3 = Boolean.parseBoolean(value2);
		Boolean booleanValue4 = Boolean.valueOf(value2);
		
		System.out.println("booleanValue3: "+booleanValue3);
		System.out.println("booleanValue4: "+booleanValue4);
		
		//both methods return false
		Boolean booleanValue5 = Boolean.parseBoolean(value3);
		Boolean booleanValue6 = Boolean.valueOf(value3);
		
		System.out.println("booleanValue5: "+booleanValue5);
		System.out.println("booleanValue6: "+booleanValue6);
		
		//both methods return false
		Boolean booleanValue7 = Boolean.parseBoolean(value4);
		Boolean booleanValue8 = Boolean.valueOf(value4);
		
		System.out.println("booleanValue7: "+booleanValue7);
		System.out.println("booleanValue8: "+booleanValue8);
		
		//both methods return false when string other than true or false is passed
		Boolean booleanValue9 = Boolean.parseBoolean(value5);
		Boolean booleanValue10 = Boolean.valueOf(value5);
		
		System.out.println("booleanValue9: "+booleanValue9);
		System.out.println("booleanValue10: "+booleanValue10);
		
		
		//both methods return false when null is passed as argument
		Boolean booleanValue11 = Boolean.parseBoolean(value6);
		Boolean booleanValue12 = Boolean.valueOf(value6);
		
		System.out.println("booleanValue11: "+booleanValue11);
		System.out.println("booleanValue12: "+booleanValue12);
		
	}
}

Output

booleanValue1: true
booleanValue2: true
booleanValue3: true
booleanValue4: true
booleanValue5: false
booleanValue6: false
booleanValue7: false
booleanValue8: false
booleanValue9: false
booleanValue10: false
booleanValue11: false
booleanValue12: false