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
I came across an interesting behaviour of threads in James Roper’s Blog (http://jazzy.id.au/pebble/2009/04/24/java_concurrency_and_volatile.html). Tested it on Vista 64 bit machine and without a volatile keyboard, in my machine also the code goes into an infinite loop.
public class ThreadExperiement implements Runnable {
private volatile String str;
void setStr(String str)
{
this.str = str;
}
public void run()
{
while (str == null){}
System.out.println(str);
}
public static void main(String[] args) throws Exception
{
ThreadExperiement threadExp = new ThreadExperiement();
new Thread(threadExp).start();
Thread.sleep(1000);
threadExp.setStr("Hello world!!");
}
}
Put a print statement inside the while loop and it starts working. Strange , isn’t it?
Is it because of byte code optimisation which detects that there is nothing to do inside the loop ?
Ever wondered when we print xml, instead of printing it in single line can’t we print with proper formatting and indentation. If you are using Jaxb then there is a nice little trick in to achieve that.
JAXBContext jc = JAXBContext.newInstance( "MyClass.class" );
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
marshaller.marshal( <object_to_marshal>, System.out );

I was looking for some tutorial about configuring Squirrel SQL (http://squirrel-sql.sourceforge.net/) to connect to firebird database and unfortunately i couldn’t find one . So when i finally managed to connect, i thought of publishing the steps in my log so that it could be useful to some one else.
Configuring Squirrel Sql to connect to firebird was much easier than i thought. You need to go to firebird website and look for JCA-JDBC Driver downloads. At the time of writing this blog, the latest release was 2.1.6 (http://prdownloads.sourceforge.net/firebird/Jaybird-2.1.6JDK_1.6.zip?download). Download it and unzip it. There will be a jar file called Jaybird-2.1.6-full.jar. Copy this file and save it in your Squirrel lib directory (It will be something like C:/Program Files/Squirrel SQL/ lib).
Now start Squirrel and you will see the jaybird driver listed in the drivers window (with a tick sign). Now click on the menu and click add. In the Database driver section put Jaybird Driver and provide the database url in the format jdbc:firebirdsql:localhost/3050:/firebird/test.gdb Now give your username and password . Hooray !!! you are finally connected to firebird database…
