Easily get an integer from a system property in Java

Getting an integer from a system property (parameter starting with -D) in Java is very easy. Integer getInteger (String, int) Just use the method.

Demo.java


public class Demo {

    //In this sample myapp.loop.Let it be a system property called count
    private static final String SYSTEM_PROP_KEY = "myapp.loop.count";

    public static void main(String[] args) {

        //★ Point
        Integer count = Integer.getInteger(SYSTEM_PROP_KEY, 10);
        String value = "";
        for (int i = 0; i < count; i++) {
            value = value + "*";
            System.out.println(value);
        }
    }
}

** ★ Point ** The first argument is the system property key (character string), and the second argument is the default value. The default value is applied when the system property is not set or when the set value cannot be converted to Integer.

Try it out


C:\temp>javac Demo.java

C:\temp>java Demo
*
**
***
****
*****
******
*******
********
*********
**********

C:\temp>java -Dmyapp.loop.count=5 Demo
*
**
***
****
*****

C:\temp>java -Dmyapp.loop.count=x Demo
*
**
***
****
*****
******
*******
********
*********
**********

C:\temp>

The first is when no system properties have been set. In this case, the default value is 10.

The second time is when 5 is set in the system properties. In this case, 5 is set firmly.

The last is when you set the system property to a value that cannot be converted to an Integer (x in the example). Again, the default value is 10.

If you want to get an integer from a system property, take the trouble to System.getProperty (String) It's easy because you don't have to use .lang.String-)!

The following is an article for newcomers. For your reference.

Recommended Posts

Easily get an integer from a system property in Java
Get a non-empty collection from an Optional stream in java
[Java] Get a random value from an array
Get attributes and values from an XML file in Java
Get stuck in a Java primer
Get history from Zabbix server in Java
How to get Class from Element in Java
Get unixtime (seconds) from ZonedDateTime in Scala / Java
Library "OSHI" to get system information in Java
[Java] Get KFunction from Method / Constructor in Java [Kotlin]
Quickly implement a singleton with an enum in Java
Call a program written in Swift from Processing (Java)
[Android development] Get an image from the server in Java and set it in ImageView! !!
Dreaming of easily creating a Web API for the DB of an existing Java system
Get EXIF information in Java
Get weather forecasts from Watson Weather Company Data in simple Java
Find a subset in Java
How to store a string from ArrayList to String in Java (Personal)
[Java] Get KClass in Java [Kotlin]
Take a look at Kotlin from an Effective Java perspective
Generate Stream from an array of primitive types in Java
About the method to convert a character string to an integer / decimal number (cast data) in Java
I tried to create an API to get data from a spreadsheet in Ruby (with service account)
[Java] Get data from DB using singleton service in Spring (Boot)
[Note] What I learned in half a year from inexperienced (Java)
Cast an array of Strings to a List of Integers in Java
[Note] What I learned in half a year from inexperienced (Java) (3)
Get the public URL of a private Flickr file in Java
How to get the length of an audio file in java
Effective Java Item 25 Select a list from an array First half
I sent an email in Java
Easily read text files in Java (Java 11 & Java 7)
3 Implement a simple interpreter in Java
I created a PDF in Java.
Get country from IP address (Java)
Get Null-safe Map values in Java
Run a batch file from Java
Access Teradata from a Java application
Try an If expression in Java
A simple sample callback in Java
I made an annotation in Java.
Run an external process in Java
How to get the setting value (property value) from the database in Spring Framework
How to get the absolute path of a directory running in Java
I want to ForEach an array with a Lambda expression in Java
When calling println etc. from an external Java class file in Processing
About returning a reference in a Java Getter
Study Deep Learning from scratch in Java.
What is a class in Java language (3 /?)
When seeking multiple in a Java array
Get the result of POST in Java
Call Java method from JavaScript executed in Java
Re-study Docker from a system operation perspective
OCR in Java (character recognition from images)
Get caller information from stack trace (java)
Reverse Key from Value in Java Map
[Creating] A memorandum about coding in Java
Using JavaScript from Java in Rhino 2021 version
Java creates a table in a Word document
Java creates a pie chart in Excel
What is a class in Java language (1 /?)