How to convert String to boolean

In Java we have multiple ways to convert String to boolean. We will see a few of those ways in this post.

1. Convert String to boolean using Boolean.parseBoolean()

For Any value of string other than “true” (case insensitive), parseBoolean() will return false.

Output

2. Convert String to boolean using Boolean.valueOf()

Similar to Boolean.parseBoolean(), we can convert String to Boolean using Boolean.valueOf().

Output

Difference between Boolean.parseBoolean() and Boolean.valueOf()

The major difference between parseBoolean() and valueOf() is their return values.
parseBoolean will return primitive boolean, whereas valueOf() returns Boolean object.

References

1. Boolean API