What is the purpose of the System class

The purpose of the System class is to provide access to system resources.

The System class in Java is a part of the java.lang package and serves several purposes. Some of the main purposes of the System class include:

  1. Standard Input/Output: The System class provides access to the standard input, output, and error streams through its in, out, and err variables. For example, System.out.println("Hello, World!"); is commonly used to print output to the console.
    java
    System.out.println("Hello, World!");
  2. System Properties: The System class allows access to system properties, which are key-value pairs representing the configuration of the current Java Virtual Machine (JVM) and the underlying system. Methods like getProperty and getProperties are used to retrieve information about the system.
    java
    String javaVersion = System.getProperty("java.version");
  3. System-wide Resource Management: The System class has methods for garbage collection (gc()), terminating the application (exit()), and getting the current time in milliseconds (currentTimeMillis()).
    java
    long currentTime = System.currentTimeMillis();
  4. Array Copying: The arraycopy method in the System class is used for efficiently copying elements from one array to another.
    java
    int[] sourceArray = {1, 2, 3, 4, 5};
    int[] destinationArray = new int[5];
    System.arraycopy(sourceArray, 0, destinationArray, 0, sourceArray.length);

These are some of the primary functionalities provided by the System class in Java.