Java Program to convert string type variables into boolean

To understand this example, you should have the knowledge of the following Java programming topics:


Example 1: Convert string to boolean using parseBoolean()

In the above example, we have used the parseBoolean() method of the Boolean class to convert the string variables into boolean.

Here, Boolean is a wrapper class in Java. To learn more, visit the Java Wrapper Class.


Example 2: Convert string to boolean using valueOf()

We can also convert the string variables into boolean using the valueOf() method. For example,

In the above example, the valueOf() method of Boolean class converts the string variables into boolean.

Here, the valueOf() method actually returns an object of theĀ Boolean class. However, the object is automatically converted into a primitive type. This is called unboxing in Java. To learn more, visit Java autoboxing and unboxing.

That is,

// valueOf() returns object of Boolean
// object is converted onto boolean value
boolean b1 = Boolean obj = Boolean.valueOf(str1)