Boolean.parseBoolean() method in Java

This post will discuss the parseBoolean() method of the Boolean Wrapper class in detail. It’s a static method and was introduced in Java 1.5.

  • Method declaration – public static boolean parseBoolean(String s)
  • What does it do? parseBoolean() method parses the string argument as a boolean.
  • What does it return? It will return the boolean primitive value represented by the string argument.

parseBoolean() will return true if the string passed is “true” and will return false on every other string passed.

Code Example
public class Codekru {

	public static void main(String[] args) {

		System.out.println("booelan value 1: " + Boolean.parseBoolean("true"));
		System.out.println("booelan value 2: " + Boolean.parseBoolean("false"));
		System.out.println("booelan value 3: " + Boolean.parseBoolean("abc"));

	}
}

Output –

booelan value 1: true
booelan value 2: false
booelan value 3: false

We can see that parseBoolean(“abc”) returned false.

Internal implementation of the parseBoolean() method
    public static boolean parseBoolean(String s) {
        return "true".equalsIgnoreCase(s);
    }

parseBoolean() method internally uses the equalsIgnoreCase method of the String class. So, parseBoolean() method will return true only if the equalsIgnoreCase() method does so.

And we also got to know that we can pass “true” in any case ( uppercase, lowercase or mixed case), and parseBoolean() will still return true.

parseBoolean("TRUE") -> true
parseBoolean("true") -> true
parseBoolean("True") -> true
parseBoolean("TrUe") -> true
Time complexity of the parseBoolean() method

parseBoolean() method internally uses the equalsIgnoreCase() method. So, the time complexity of the parseBoolean() depends on the time complexity of the equalsIgnoreCase() method.

equalsIgnoreCase() has the worst-case time complexity of O(n), where n is the length of the string. The worst-case time complexity of the parseBoolean() method would be O(4) only.
It will happen when we pass “true” as an argument in the method.

Why the worst-case time complexity is O(4)? equalsIgnoreCase() first compares the length of the strings, and if they mismatch, then it would return false there only. So, the time complexity would be O(1).
When we use a string argument in the parseBoolean() method whose length is greater or even less than 4, then it would mismatch with the “true” string’s length, and parseBoolean() would return false in O(1) time complexity.

parseBoolean("true") -> O(4) time complexity
parseBoolean("false")-> O(1) time complexity
parseBoolean("abc")> O(1) time complexity
What if we passed a null object in the parseBoolean() function argument?

The function will return false, as illustrated by the below example –

public class Codekru {

	public static void main(String[] args) {

		System.out.println("booelan value: " + Boolean.parseBoolean(null));
	}
}

Output –

booelan value: false

Please visit this link to learn more about the Boolean wrapper class of java and its other functions or methods.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *