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 of InputStream).

  • System.out: Standard output stream (an instance of PrintStream).

  • System.err: Standard error stream (an instance of PrintStream).

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

Property Key
Description

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 a Properties object containing all system properties.

  • System.setProperties(Properties props): Sets the system properties to the specified Properties object.

Example: Accessing System Properties

public class SystemPropertiesExample {
    public static void main(String[] args) {
        // Accessing common system properties
        System.out.println("Java Version: " + System.getProperty("java.version"));
        System.out.println("OS Name: " + System.getProperty("os.name"));
        System.out.println("User Name: " + System.getProperty("user.name"));

        // Setting and getting a custom property
        System.setProperty("my.custom.property", "customValue");
        System.out.println("Custom Property: " + System.getProperty("my.custom.property"));
    }
}

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

public class EnvironmentVariablesExample {
    public static void main(String[] args) {
        // Accessing a specific environment variable
        String path = System.getenv("PATH");
        System.out.println("PATH: " + path);

        // Accessing all environment variables
        Map<String, String> env = System.getenv();
        for (String envName : env.keySet()) {
            System.out.println(envName + " = " + env.get(envName));
        }
    }
}

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

public class SystemUtilityMethodsExample {
    public static void main(String[] args) {
        // Array copying
        int[] sourceArray = {1, 2, 3, 4, 5};
        int[] destinationArray = new int[5];
        System.arraycopy(sourceArray, 0, destinationArray, 0, sourceArray.length);
        System.out.println("Copied Array: " + java.util.Arrays.toString(destinationArray));

        // Getting current time in milliseconds
        long currentTimeMillis = System.currentTimeMillis();
        System.out.println("Current Time (ms): " + currentTimeMillis);

        // Getting current time in nanoseconds
        long currentNanoTime = System.nanoTime();
        System.out.println("Current Time (ns): " + currentNanoTime);

        // Suggesting garbage collection
        System.gc();
        System.out.println("Garbage collection suggested.");
    }
}

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

public class ExitExample {
    public static void main(String[] args) {
        System.out.println("Exiting the program with status 0.");
        System.exit(0);
    }
}

Last updated

Was this helpful?