How to convert String
object to Boolean
object?
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. 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.
To convert Boolean to String in Java, use the toString() method.
To convert String to boolean in Java, you can use Boolean. parseBoolean(string). But if you want to convert String to Boolean object then use the method Boolean. valueOf(string) method.
We can convert String to boolean in java using Boolean. parseBoolean(string) method. To convert String into Boolean object, we can use Boolean. valueOf(string) method which returns instance of Boolean class.
Try (depending on what result type you want):
Boolean boolean1 = Boolean.valueOf("true"); boolean boolean2 = Boolean.parseBoolean("true");
Advantage:
Boolean.TRUE
or Boolean.FALSE
.The official documentation is in the Javadoc.
UPDATED:
Autoboxing could also be used, but it has a performance cost.
I suggest to use it only when you would have to cast yourself, not when the cast is avoidable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With