devxlogo

An Easy Way to Check Boolean System Properties

An Easy Way to Check Boolean System Properties

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
See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist