java.lang.System
About
The java.lang.System
class is a crucial part of the Java Standard Library, providing a collection of static methods and fields that allow interaction with the environment in which the Java application is running.
The System
class cannot be instantiated. It provides various useful class fields and methods to handle standard input, output, and error streams; access to system properties and environment variables; and utility methods for array copying, garbage collection, and time measurement.
Standard Streams
The System
class provides three standard I/O stream objects:
System.in
: Standard input stream (an instance ofInputStream
).System.out
: Standard output stream (an instance ofPrintStream
).System.err
: Standard error stream (an instance ofPrintStream
).
System Properties
System properties are key-value pairs that provide information about the runtime environment. These properties can be accessed and modified using the System.getProperty
, System.setProperty
, and System.getProperties
methods.
Commonly Used System Properties
java.version
Java Runtime Environment version
java.vendor
Java Runtime Environment vendor
java.vendor.url
Java vendor URL
java.home
Java installation directory
java.vm.specification.version
Java Virtual Machine specification version
java.vm.specification.vendor
Java Virtual Machine specification vendor
java.vm.specification.name
Java Virtual Machine specification name
java.vm.version
Java Virtual Machine implementation version
java.vm.vendor
Java Virtual Machine implementation vendor
java.vm.name
Java Virtual Machine implementation name
java.specification.version
Java Runtime Environment specification version
java.specification.vendor
Java Runtime Environment specification vendor
java.specification.name
Java Runtime Environment specification name
java.class.version
Java class format version number
java.class.path
Java class path
java.library.path
List of paths to search when loading libraries
java.io.tmpdir
Default temp file path
java.compiler
Name of JIT compiler to use
java.ext.dirs
Path of extension directory or directories
os.name
Operating system name
os.arch
Operating system architecture
os.version
Operating system version
file.separator
File separator ("/" on UNIX, "" on Windows)
path.separator
Path separator (":" on UNIX, ";" on Windows)
line.separator
Line separator ("\n" on UNIX, "\r\n" on Windows)
user.name
User account name
user.home
User home directory
user.dir
User's current working directory
Accessing System Properties
We can access and modify system properties using the following methods:
System.getProperty(String key)
: Returns the value of the specified system property.System.getProperty(String key, String defaultValue)
: Returns the value of the specified system property, or the default value if the property is not found.System.setProperty(String key, String value)
: Sets the system property to the specified value.System.getProperties()
: Returns aProperties
object containing all system properties.System.setProperties(Properties props)
: Sets the system properties to the specifiedProperties
object.
Example: Accessing System Properties
Environment Variables
We can access environment variables using the System.getenv
method:
System.getenv(String name)
: Returns the value of the specified environment variable.System.getenv()
: Returns a map of all environment variables.
Example: Accessing Environment Variables
Utility Methods
Array Copying
System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
: Copies elements from one array to another.
Garbage Collection
System.gc()
: Suggests that the JVM performs garbage collection.
Current Time
System.currentTimeMillis()
: Returns the current time in milliseconds since the epoch (January 1, 1970, 00:00:00 GMT).System.nanoTime()
: Returns the current value of the running JVM's high-resolution time source, in nanoseconds.
Example: Using Utility Methods
Exiting the JVM
System.exit(int status)
: Terminates the currently running JVM. A non-zero status code indicates abnormal termination, while a zero status code indicates normal termination.
Example: Exiting the JVM
Last updated
Was this helpful?