


valueOf returns a Boolean object String data = "false" ītw, there is another way to convert String to Boolean in Java, by using the constructor of the Boolean class e.g. parseBoolean returns a boolean primitive value String value = "true" īoolean b = Boolean.parseBoolean( value) parseBoolean(null) and valueOf(null) will return false instead of throwing NullPointerExcpeiton. It also returns a Boolean object instead of a primitive boolean value.Īlso, both valueOf() and parseBoolean() methods are null safe, which means they will return false if you pass null i.e. The Boolean.valueOf() method works similarly, it returns a true boolean value for a non-null String equal to true, ignoring the case, and returns false for everything else. For example, if you pass "YES" it will return false, which is not obvious but that's still better than throwing an exception like NumberFormatException. The good thing about this method is that it is case insensitive, which means if you pass "true", " TRUE", or "True" you will still get a true boolean value.Īnother good thing about this method is that it doesn't throw an exception if you pass any String value other than true and false.

Similarly, if you pass "false" it will return false. The parseBoolean() method returns an equivalent boolean value of a given String, for example, if you pass "true" it will return the primitive boolean value true. There are two ways to convert a String to a boolean in Java, first, by using Boolean.parseBoolean() method, and second, by using Boolean.valueOf() method.
