Java System Class

Introduction

The System class in Java provides various utility methods and system-related functionalities such as standard input, output, and error streams, environment variables, and more.

Table of Contents

  1. What is the System Class?
  2. Common Methods
  3. Examples of Using the System Class
  4. Conclusion

1. What is the System Class?

The System class is a final class that cannot be instantiated. It contains static methods and fields to interact with the environment, manage system properties, and handle input/output streams.

2. Common Methods

  • arraycopy(Object src, int srcPos, Object dest, int destPos, int length): Copies an array from the specified source array to the specified position of the destination array.
  • currentTimeMillis(): Returns the current time in milliseconds since January 1, 1970, UTC.
  • exit(int status): Terminates the current JVM.
  • gc(): Suggests the JVM to perform garbage collection.
  • getProperty(String key): Returns the system property associated with the specified key.
  • setProperty(String key, String value): Sets the system property indicated by the specified key.
  • getenv(String name): Retrieves the value of the specified environment variable.
  • nanoTime(): Returns the current value of the running JVM's high-resolution time source in nanoseconds.

3. Examples of Using the System Class

Example 1: Using arraycopy

This example copies elements from a source array to a destination array.

public class ArrayCopyExample {
    public static void main(String[] args) {
        int[] source = {1, 2, 3, 4, 5};
        int[] destination = new int[5];

        System.arraycopy(source, 0, destination, 0, source.length);

        for (int num : destination) {
            System.out.print(num + " ");
        }
    }
}

Output:

1 2 3 4 5

Example 2: Using currentTimeMillis

This example retrieves the current time in milliseconds since the Unix epoch.

public class CurrentTimeMillisExample {
    public static void main(String[] args) {
        long currentTime = System.currentTimeMillis();
        System.out.println("Current time in milliseconds: " + currentTime);
    }
}

Output:

Current time in milliseconds: 1719747163937

Example 3: Using exit

This example demonstrates how to terminate the JVM using System.exit.

public class ExitExample {
    public static void main(String[] args) {
        System.out.println("Exiting the program.");
        System.exit(0);
        // Code below will not be executed
    }
}

Output:

Exiting the program.

Example 4: Using gc

This example requests the JVM to perform garbage collection.

public class GarbageCollectionExample {
    public static void main(String[] args) {
        System.out.println("Requesting garbage collection.");
        System.gc();
    }
}

Output:

Requesting garbage collection.

Example 5: Using getProperty

This example retrieves the Java version system property.

public class GetPropertyExample {
    public static void main(String[] args) {
        String javaVersion = System.getProperty("java.version");
        System.out.println("Java Version: " + javaVersion);
    }
}

Output:

Java Version: 21.0.2

Example 6: Using setProperty

This example sets a custom system property and retrieves it.

public class SetPropertyExample {
    public static void main(String[] args) {
        System.setProperty("custom.property", "CustomValue");
        String customProperty = System.getProperty("custom.property");
        System.out.println("Custom Property: " + customProperty);
    }
}

Output:

Custom Property: CustomValue

Example 7: Using getenv

This example retrieves the value of the PATH environment variable.

public class GetEnvExample {
    public static void main(String[] args) {
        String path = System.getenv("PATH");
        System.out.println("PATH Environment Variable: " + path);
    }
}

Output:

PATH Environment Variable: /opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

Example 8: Using nanoTime

This example measures the elapsed time of operations in nanoseconds.

public class NanoTimeExample {
    public static void main(String[] args) {
        long startTime = System.nanoTime();
        // Perform some operations
        long endTime = System.nanoTime();
        System.out.println("Elapsed time in nanoseconds: " + (endTime - startTime));
    }
}

Output:

Elapsed time in nanoseconds: 83

4. Conclusion

The System class in Java provides essential utilities for interacting with the runtime environment, managing system properties, and handling input/output. By leveraging its methods, developers can effectively manage system-level tasks in their applications.

Comments