Last Updated:

Working with the Java System Class (java.lang.System)

Java System Class briefly

The class is final, all fields and methods are static, so we can't create a subclass and override its methods using inheritance. The class does not expose any public constructors, so we cannot create an instance of this System

Example of working with a class

Calling a method from a class looks like this: - This is one of the easiest ways to log

Next, let's look at the various functions provided by the .java.lang.System

Example of using Array Copy of the Java System class

The System class provides a native method for copying data from one array to another. This is a built-in method, so it should work faster than other methods of copying an array.

The method throws an exception if the copy accesses data outside the array boundaries.System.arraycopyIndexOutOfBoundsException

It also throws if an element in the source array cannot be stored in the destination array due to a type mismatch.ArrayStoreException

A NullPointerException can also be thrown if the source array or the destination array.null

The following is an example of a program with an example of how to use this method:



Example of using System Properties

The System class contains a very useful method for retrieving System properties and working with them further. This link provides an example of how to use the various System properties methods.

Reading from the Console/Writing to the Console Using the Java System Class

The System class provides a convenient way to get a unique object associated with . The Console class was introduced in Java IO version SE 1.6 with a useful method for printing formatted data and safely reading a password. If your console is not associated with the current JVM or is running as a background program, null is returned to try to get the Console object.ConsoleJVM

The following is a utility for retrieving an object of the Console class using:

Get the current time by using the System class

The System class provides two methods for retrieving the current time in milliseconds and nanoseconds. We can use time in milliseconds to create a Date object. Time in nanoseconds is used mainly in scientific experiments or program performance testing.

The following is a code snippet that shows how to use the methods of the System class to get the current time.

The System class. Environment variables

The System class provides a method for retrieving environment variables as an immutable Map that contains key-value pairs for each environment variable.

We work with Security Manager

The class is used to implement the security policy of your application, the System class provides useful methods for obtaining for the currently running and setting the necessary settings.SecurityManagerSecurityManagerJVM

The System class contains three fields, , and . They are used to read data from and write data to the . For example, we can set FileOutputStream to the out and err fields so that the result of the output to the console is written to a file.inouterrInputStreamOutputStream

Advanced operations. System class

The System class provides many methods for advanced operations, such as starting the garbage collector, loading an external library, and shutting down the JVM.

//start the garbage collector
System.out.println("Garbage collector started");

//load external libraries
//terminate the current JVM
// and this output to the console will never happen,
// because the JVM has already finished its work
System.out.println("JVM is terminated");

These were the most interesting methods of the System class in Java. Hopefully this will help you better understand and use the System class more in the future.