Java System.getProperty() Method

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
154883
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
154883
5
Start Learning
Topics Covered

Overview

The Java System.getProperty method is a built-in method of the System class in Java which is used to return the property of a system that is read by the key specified.

The system is a final class that contains several useful class fields and methods. out is a field in the System class, which is of type PrintStream, and it's the "standard" output stream. and println() is the method defined in PrintStream class

Syntax of Java System.getProperty()

The following is the syntax of the java system getproperty methods:

  • getProperty method with single parameter:
  • getProperty method with two parameters:

Parameters of Java System.getProperty()

getProperty Method with Single Parameter

In this case, the getProperty method takes one parameter key of the string type.

  • key: Key refers to the value whose system properties are being extracted.

getProperty Method with Two Parameters

In this case, the getProperty method takes two parameters key and definition of string type.

  • key: Key refers to the value whose system properties are being extracted.
  • definition: Definition refers to the default value of the specified key.

Return Value of Java System.getProperty()

The java system getproperty methods, when called, return the following values:

  • It returns the system property of the key specified in the parameter.
  • If the key is not present in the string input then it returns the null value.

Example

getProperty Method with Single Parameter

In this example, we will pass a string "java getProperty:" and a key "Java" in the getProperty method and will use the java system getproperty.

Code:

Output:

Explanation of the example:

  • In this case, we are only passing one parameter "Java" to the java system getproperty method, thus the System.out.println("Java getProperty : " + System.getProperty("Java")); would display null because the definition of the key is not present.

getProperty Method with Two Parameters

In this example, we will pass a string "java getProperty: " and a key "Java" and definition "language" in the getProperty method and will use the java system getproperty.

Code:

Output:

Explanation of the example:

  • In this case, we are passing two parameters "Java" and "language" to the java system getproperty method, thus the System.out.println("Java getProperty : " + System.getProperty("Java", "language")); would display 'Java getProperty : language'.

What is Java System.getProperty()?

As discussed earlier, The java system getproperty method is used to return the property of a system that is read by the key specified.

The java system getproperty is a method of Java System class. The java system getproperty method is passed an argument list and it is used to extract the property associated with keys from the list.

There are two types of java system getproperty methods present in the Java System Class:

  • java system getproperty method with single parameter: This method only takes one parameter key and returns the values associated with the key as a string value.
  • java system getproperty method with two parameters: This method only takes two parameters key and definition and returns the values associated with the key as a string value.

More Examples

In this section, we will be going displaying the system properties of custom values as well as inbuilt ones.

Output:

Explanation

In the above example, for each case, we have passed the key and definitions (in the case of two parameters). The java system getproperty method will return the values according to the input provided.

Java System.getProperty vs System.getenv

The System.getenv() returns an unmodifiable string map view of the current system environment. In this section we will learn the difference between System.getProperty vs System.getenv:

System.getPropertySystem.getenv
The System.getProperty values are available only within the scope of the Java platform.The System.getenv values are globally scoped at the Operating System level and thus available to all applications running on the same machine.
It is necessary to exit System.getProperty while packaging the application.The System.getenv values can be created anytime.
The System.getProperty values can be updated at runtime.The System.getenv values are an immutable copy of the operating system thus they cannot be updated.

Conclusion

  • The java system getproperty method is used to return the property of a system.
  • The java system getproperty works on key-value pairs.
  • The java system getproperty is a method of Java system class.
  • The java system getproperty methods can either accept one parameter key or two parameters key and definition.
  • The java system getproperty returns the system property of the specified key.