Shankh is London based consultancy started on 2011 to provide consultancy services primarily in Java. With several successful Java projects under our belt, we started exploring mobile based services towards end of 2012 which led to our first android/ios/windows-phone app which is staged for release in 2013

Some Random quotes


Innovation distinguishes between a leader and a follower. -Steve Jobs

It is not that i am smarter than others, i just persist with problems longer

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

Share

12 Responses to “Some fun with Boolean.getBoolean”

  1. Dennis Says:

    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.

  2. Garidan Says:

    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

  3. admin Says:

    @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

  4. Pete Says:

    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.

  5. admin Says:

    @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….

  6. Tony Morris Says:

    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.

  7. Peter Lawrey Says:

    You could define -Dtrue=true on the command line to add to the confusion. ;)

  8. Arsalan Says:

    Thanks for sharing. I got fooled by its name and used in my code which didnt work anyway.. Your post helped me out.

  9. marino Says:

    thanks for this post. Save me time and headache :-)

  10. ankit Says:

    You can also use valueOf(boolean b)

  11. Rashi..Coorg.. Says:

    Thanks a lot.. :) CLarified my problem as i was wonder with the output produced by this method when i used it..

  12. Zilvinas Says:

    You should use “parseBoolean” when trying to parse a “boolean” from string value to an actual boolean object ;)

    While the “getBoolean” method gets a system property and then tries to parse it to a boolean value.

Leave a Reply

Subscribe to Comments?



RECENTPOSTS

MOSTCOMMENTS

MYARCHIVE