This post will explore the difference between System properties and environment variables in Java.

1. What are System properties

System properties in Java are key-value pairs that describe the configuration of the current working environment. They can be accessed and modified by the System class, which provides methods to get and set system properties. They are declared with the -Dname=value syntax on the Java command line or added at runtime using System.setProperty(key, value) method. They can be accessed using System.getProperty(key) or System.getProperty(key, def).

We can also list all the system properties by using the System.getProperties() method, which returns a Properties object containing all the key-value pairs. Let’s see an example of how to create and use System properties in Java:

Download  Run Code

2. What are environment variables

Environment variables are variables that are set in the Operating System and passed to the applications running on the same machine. They can be set system-wide or by specific users. Different applications are interested in different environment variables.

Java just happens to be one of the applications running on the OS. A Java application can get access to environment variables is Java using the System.getenv() method. Let’s see an example of how to create and use environment variables in Java:

Download  Run Code

3. Differences between System properties and environment variables

Here are some of the main differences between System properties and environment variables in Java:

1. Set

System properties are variables that are declared with the -Dname=value syntax on the command-line options or added at runtime using System.setProperty(String key, String value) or via the various System.getProperties().load() methods. The environment variables are set in the operating system. They can be set using the EXPORT command in Unix and SET command in windows.

2. Retrieval

To retrieve a specific System property, we can use the System.getProperty(name) or System.getProperty(key, def) methods. To fetch a specific environment variable, call the System.getenv(name) or System.getenv().get(name) methods.

3. Runtime manipulate

System properties can be added at runtime using System.setProperty() method or with the help of System.getProperties().load() methods. On the other hand, environment variables cannot be set at runtime.

4. Access

System properties are accessible by the process they are added to, while any process can access environment variables. System properties can be added, modified, or removed at runtime by using the System class methods and it will not impact other applications running on the same machine. Environment variables cannot be added, modified, or removed at runtime by using the System class methods. They are common for all other applications running on the same machine.

5. Usage

System properties can be used to configure or customize the behavior of the Java platform or application. For example, System properties can be used to store constants or global variables that are specific to the application. Environment variables can be used to configure or customize the behavior of the Operating System or application. For example, Environment variables can be used to store constants or global variables that are not specific to the application.

4. What to use and when?

As we have seen, System properties and environment variables are both useful types of variables to store and process data in Java. Here are some general recommendations on how to choose between them:

  • If you need to store a piece of data that is common to all applications running on the same machine and does not change over time, we can use an Environment Variable.
  • If you need to store a piece of data that is specific to the Java platform or application and may change over time, we should use a System property.

That’s all about the differences between System properties and environment variables in Java.