Suppose you want to pass a Boolean system property, -DDEBUG=true or -DDEBUG=false, from the command line. Normally, you’d probably use the following in source code to retrieve it:
boolean b = Boolean.valueOf(System.getProperty("DEBUG")).booleanValue();if(b) { //do something;}or just treat it as a string system property:if(System.getProperty("DEBUG").equalsIgnoreCase("true")) { //do something;}
The following code offers an easier way to do the same thing:
public class GetBoolean { public static void main(String args[]) throws Exception { if(Boolean.getBoolean("DEBUG")) { System.out.println("Debug is on"); } else { System.out.println("Debug is off"); } }}
The GetBoolean class is tested under JDK1.3.1 with the following output:
D:foo>java -DDEBUG=true GetBooleanDebug is onD:foo>java -DDEBUG=false GetBooleanDebug is offD:foo>java -DDEBUG=TRue GetBooleanDebug is onD:foo>java -DDEBUG=Yes GetBooleanDebug is offD:foo>java GetBooleanDebug is off