Some fun with Boolean.getBoolean
What will be the output of this code snippet ?
public class BooleanFun {
public static void main(String[] args) {
Boolean status = Boolean.getBoolean("true");
System.out.println("First Case:"+status);
}
}
You may think that it will obviously print First Case:true.. But it is not that straightforward. The output is following
First Case:false
Well .. How did this happen? Boolean.getBoolean property does something else than what its name denotes. According to its Javadoc
” Boolean.getBoolean Returns true if and only if the system property named by the argument exists and is equal to the string ‘true’. (Beginning with version 1.0.2 of the JavaTM platform, the test of this string is case insensitive.) A system property is accessible through getProperty, a method defined by the System class. ”
See the following code snippet to understand how it works..
public class BooleanFun {
public static void main(String[] args) {
Boolean status = Boolean.getBoolean("true");
System.out.println("First Case:"+status);
System.setProperty("bfun", "true");
String bfunString = System.getProperty("bfun");
System.out.println("System Property bfun value:"+bfunString);
status = Boolean.getBoolean("bfun");
System.out.println("Second Case:"+status);
System.setProperty("bfun2", "random");
String bfun2String = System.getProperty("bfun2");
System.out.println("System Property bfun2 value:"+bfun2String);
status = Boolean.getBoolean("bfun2");
System.out.println("Third Case:"+status);
}
}
Output
First Case:false
System Property bfun value:true
Second Case:true
System Property bfun2 value:random
Third Case:false
So Boolean.getBoolean will check whether the System property of a given name exists and also whether its value is “true”. In only this case will it return true..
Hopefully this information will help somebody trying to figure out why their program always returns false for Boolean.getBoolean

July 10th, 2009 at 12:47 pm
Actually it is documented in JavaDoc:
public static boolean getBoolean(String name)
Returns true if and only if the system property named by the argument exists and is equal to the string “true”. (Beginning with version 1.0.2 of the JavaTM platform, the test of this string is case insensitive.) A system property is accessible through getProperty, a method defined by the System class.
If there is no property with the specified name, or if the specified name is empty or null, then false is returned.
July 10th, 2009 at 3:08 pm
What on earth that Sun developer was thinking about when writing such an idiot choice ?
Really stupid and dangerous
Thnaks for this, and i find strange this method was not deprecated immediatly after first release.
Bye
July 10th, 2009 at 4:35 pm
@Dennis
Yes it is documented but developers sometimes tend to overlook the documentation and go by the name of the method which in this case is a bit unclear.
@Garidan
They should have removed it after 1.3.1. Unfortunately the deprecation policy changed during that time and only methods which were inherently unsafe was selected for deprecation. See this Bug report http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4533891
In my opinion this is a critically unsafe method
July 10th, 2009 at 6:36 pm
What the hell?
It’s a perfectly valid, very useful, method.
Calls to deprecate it are bizarre! Any moron who misuses it because they can’t read Javadoc shouldn’t be hired in the first place.
converting a string literal into a boolean is what the valueOf method is for.
July 11th, 2009 at 4:03 am
@Pete
It is not really a method to convert string literal into boolean. It is a method to convert the ‘true/any_other_value’ of a system property to Boolean.true/false.
This method may be useful in certain situations. What my point is that the name of the method is not clear and causes confusion. Programmers are human and they tend to make mistakes. I haven’t seen a single programmer in my whole career who hasn’t made mistakes. One of the best practices in programming is to give an appropriate name to your methods. If your method name is ambiguous you are inviting more bugs to the system (blame it on bad programming… but unfortunately that exists)
Imagine, if the method was having some sensible name like Boolean.getSystemPropertyAsBoolean … That will clear all confusion and will get rid of half of the bugs caused by this invalid naming.
Bottom line is keep it simple….
July 13th, 2009 at 3:15 am
The function gives the illusion that it acts only on its given arguments. Instead, it acts on the outside world. In more practical languages, this is denoted by the type system making this potential error disappear along with many others.
July 19th, 2009 at 2:31 am
You could define -Dtrue=true on the command line to add to the confusion.
February 11th, 2012 at 6:16 pm
Thanks for sharing. I got fooled by its name and used in my code which didnt work anyway.. Your post helped me out.
May 24th, 2012 at 11:57 am
thanks for this post. Save me time and headache
July 2nd, 2012 at 11:31 am
You can also use valueOf(boolean b)
November 15th, 2012 at 11:56 pm
Thanks a lot..
CLarified my problem as i was wonder with the output produced by this method when i used it..