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.

  3. public static Boolean valueOf(String s), anyone?

    It follows the same naming pattern as String.valueOfs.

Leave a comment