Solved: string to boolean

String to Boolean in Java plays an integral role in various programming solutions. When dealing with user inputs, or retrieving data from databases or other sources, there’s a high probability of facing instances where the data is available in string format. In such cases, it’s necessary to have a sound knowledge of how to convert these string values into boolean.

Data type conversion is a common practice in programming. String to boolean conversion may become essential when the boolean values are wrapped in a string, and string values are to be converted into booleans to make a decision based on their values. For instance, if you have string values like “true” or “false” and based on these values some decisions have to be made in the code.

Java Solution to Convert String to Boolean

Java provides a straightforward solution to convert a String to Boolean. You can make use of the static method Boolean.parseBoolean(String s) which returns the Boolean represented by the string.

String test = "true";
boolean bool = Boolean.parseBoolean(test);
System.out.println(bool);

In the above code, test is a string variable assigned the value “true”. The Boolean.parseBoolean(test) method is used to convert the string into a boolean. The method returns a boolean type to the variable ‘bool’. The output of this code will be true.

Working Principle of Boolean.parseBoolean() method

Letโ€™s understand step by step code execution. The parseBoolean() method of Java Boolean Class is a built-in method that parses the string argument as a boolean.

1. Declaration: The java.lang.Boolean.parseBoolean() method is declared as:

public static boolean parseBoolean(String s)

2. Parameter: The string to be parsed.

Working:

* It is case insensitive, i.e., ‘True’ and ‘true’ both would be considered as true.
* The parseBoolean() function isnโ€™t case sensitive. Word ‘true’ (irrespective of case) will return true and anything else except ‘true’ will return false.
* It is a static method hence invoking the parseBoolean method on a null reference doesnโ€™t throw a NullPointerException, it returns false.

Other methods to convert String to Boolean in Java

Another method to string to boolean in Java is using Boolean.valueOf(String). This method behaves similar to Boolean.parseBoolean() method except that it returns instance of Boolean class whereas parseBoolean() method of Boolean class is a static method that returns a boolean primitive.

String test = "true";
Boolean bool = Boolean.valueOf(test);
System.out.println(bool);

In the above code, Boolean.valueOf(test) is used to convert the string into a Boolean object. The method returns a Boolean object to the variable ‘bool’. The output of this code will be ‘TRUE’.

Thus, Java gives several solutions to convert a string to boolean efficiently. These methods are particularly useful when you need to take major decisions in your application based on the boolean result. Knowing when to utilize these methods can make a lot of difference in your programming practice.

Related posts:

Leave a Comment