I am surprised to know that getBoolean()
and valueOf()
method returns different results for the same input string.
I have tried to pass the "true"
to both the methods. But getBoolean()
gives me false output whereas valueOf()
gives me right output that is true. Why?
The valueOf() method returns a Boolean instance corresponding to 'b' or to String 's'. It returns true, if the defined Boolean value (b) or String value(s) is true. It returns false, if the String contains any value other than true.
valueOf(boolean b) returns a Boolean instance representing the specified boolean value. If the specified boolean value is true, this method returns Boolean. TRUE; if it is false, this method returns Boolean.
The API-documentation is your friend.
Boolean.getBoolean
probably doesn't do what you think it does:
Returns true if and only if the system property named by the argument exists and is equal to the string "true".
Boolean.valueOf
is probably what you're looking for:
The Boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".
The javadoc of getBoolean clearly state that it searches the System Properties. The value that you pass to it is the name of the system property, not a "true" or "false" string.
E.g., `var b = Boolean.getBoolean("some.property");
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