Home >Java >javaTutorial >Interpretation of Java documentation: Usage analysis of getProperty() method of System class

Interpretation of Java documentation: Usage analysis of getProperty() method of System class

WBOY
WBOYOriginal
2023-11-03 12:22:511305browse

Interpretation of Java documentation: Usage analysis of getProperty() method of System class

Interpretation of Java documentation: Usage analysis of the getProperty() method of the System class, specific code examples are required

The System class in Java is a very important class, including Many operating methods related to the program's running environment. Among them, the getProperty() method is a more practical method, which can obtain system properties. This article will introduce the usage of the getProperty() method of the System class and provide specific code examples.

1. Method Overview

The getProperty() method of the System class returns the system property corresponding to the specified key. System properties are a series of properties set by the Java virtual machine or system when running the system. This method has two overloaded forms:

public static String getProperty(String key)
public static String getProperty(String key, String defaultValue)

The first overloaded form is used to return the value of the system property based on the specified key, or null if the key does not exist. The second overloaded form also returns the value of the system property of the specified key, but when the key does not exist, it returns the default value specified by defaultValue.

It should be noted that the getProperty() method returns the value of the system property, not the value of the environment variable. If you want to get the value of an environment variable, you need to use the getEnv() method of the System class.

2. Usage Example

The following code shows how to use the getProperty() method of the System class to obtain system properties:

String javaVersion = System.getProperty("java.version");
System.out.println("Java版本号:" + javaVersion);

String osName = System.getProperty("os.name");
System.out.println("操作系统名称:" + osName);

String userHome = System.getProperty("user.home");
System.out.println("用户主目录:" + userHome);

String userDir = System.getProperty("user.dir");
System.out.println("用户工作目录:" + userDir);

This code first uses the getProperty() method respectively The values ​​of system properties such as Java version number, operating system name, user home directory and user working directory are obtained, and then output to the console through the println() method. The output of running the above code is as follows:

Java版本号:1.8.0_231
操作系统名称:Windows 10
用户主目录:C:Usersusername
用户工作目录:D:Project

As can be seen from the output, we successfully obtained the value of the system property and output it to the console.

3. Summary

This article introduces the usage of the getProperty() method of the System class and provides specific code examples. By using this method to obtain the value of the system property, you can easily understand the relevant environment in which the current program is running. At the same time, it should be noted that the getProperty() method returns the value of the system property, not the value of the environment variable. If you want to obtain the value of the environment variable, you need to use the getEnv() method of the System class.

The above is the detailed content of Interpretation of Java documentation: Usage analysis of getProperty() method of System class. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn