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:
- Standard Input/Output: The
System
class provides access to the standard input, output, and error streams through itsin
,out
, anderr
variables. For example,System.out.println("Hello, World!");
is commonly used to print output to the console.javaSystem.out.println("Hello, World!");
- 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 likegetProperty
andgetProperties
are used to retrieve information about the system.javaString javaVersion = System.getProperty("java.version");
- 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()
).javalong currentTime = System.currentTimeMillis();
- Array Copying: The
arraycopy
method in theSystem
class is used for efficiently copying elements from one array to another.javaint[] 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.