Boolean.getBoolean not what you think it is

In Java there is a Boolean object which has a nice static method called getBoolean that takes in a String. It returns a primitive boolean. So one would think that the following would print true.

String foo = “true”;
System.out.println( “Value [” + Boolean.getBoolean( foo ) + “]” ); …

But getBoolean() does not translate a String into a boolean primitive. It gets the boolean value of a SYSTEM PROPERTY.

From the Javadoc:
Returns true if and only if the system property named by the argument exists and is equal to the string “true”.


4 thoughts on “Boolean.getBoolean not what you think it is

  1. it’s not cool – I’ve been burnt by that more times than I care to mention.
    It should have been in java.lang.System, but they’ll never move it now.

  2. What if what I think it does is what the javadoc says? Why would I think ‘oh, the Javadoc is wrong’? Unless I didn’t read it in the first place, I suppose.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s