java.lang.System Example
In this tutorial we will discuss about the System
class in Java. It contains a number of useful fields, such as the standard input, the standard output and the standard error streams. The System
class is declared as final
and thus, it cannot be instantiated.
Also, the System
class contains a large number of methods that can be used to retrieve external defined properties and environmental variables, load files and libraries, etc. Also, the System
class contains a sample method for copying a portion of an array to another array.
Finally, the System
class exists since the 1.0 version of Java.
Fields
System.in
: The standard input stream.System.out
: The standard output stream.System.err
: The standard error stream.
Important: All these streams are already open and ready to accept data.
ArrayCopyExample.java:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class ReadLineExample { public static void main(String[] args) throws IOException { BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); //Read a line from the standard input. String inputLine = rd.readLine(); // Reverse the string. StringBuilder builder = new StringBuilder(inputLine); builder.reverse(); System.out.println("Input string: " + inputLine); System.out.println("Reversed string: " + builder.toString()); System.err.println("Reversed string: " + builder.toString()); // Close the stream. rd.close(); } }
In this example we read a complete line from the standard input stream and we write that line in reversed order in both the standard output and standard error streams. A sample execution is shown below:
Input string: Hello from Java Code Geeks! Reversed string: !skeeG edoC avaJ morf olleH Reversed string: !skeeG edoC avaJ morf olleH
Array Copy
The System
class provides a general method for copying a portion of an array into another array. The method’s declaration is the following:
Let’s explain each parameter separately:
src
: the source array.srcPos
: the position in the source array from which, the copy procedure begins.dest
: the destination array.destPos
: the position in the destination array where data will be copied.length
: the number of elements to be copied.
ArrayCopyExample.java:
public class ArrayCopyExample { private final static int TOTAL_ELEMENTS = 10; public static void main(String[] args) { int[] src = new int[TOTAL_ELEMENTS]; // Populate the array with some elements. for(int i = 0; i < TOTAL_ELEMENTS; ++i) src[i] = i + 1; // Print the elements. System.out.print("Source array: "); for(int i = 0; i < TOTAL_ELEMENTS; ++i) System.out.print(src[i] + " "); System.out.println(); // Copy the array to the destination. int dst_size = src.length / 2; int[] dst = new int[dst_size]; System.arraycopy(src, 0, dst, 0, dst_size); // Print the elements. System.out.print("Destination array: "); for(int i = 0; i < dst_size; ++i) System.out.print(dst[i] + " "); System.out.println(); } }
In this example, we copy the first half of the src
array, to the dst
array.
A sample execution is shown below:
Source array: 1 2 3 4 5 6 7 8 9 10 Destination array: 1 2 3 4 5
System Properties
The System
class contains a private instance of the Properties
class, which is used to provide the configuration of the current working environment.
Important: A table containing the most important system properties can be found here.
In the following example, we print a number of system properties to the console:
SystemPropertiesExample.java:
public class SystemPropertiesExample { public static void main(String[] args) { // Prints the version of Java. System.out.println("Java version: " + System.getProperty("java.version")); // Prints the version of the underlying operating system. System.out.println("OS version: " + System.getProperty("os.version")); // Prints the user's home directory. System.out.println("Home directory: " + System.getProperty("user.home")); } }
A sample execution is shown below:
Java version: 1.8.0_20 OS version: 3.13.0-24-generic Home directory: /home/stathis
Environmental variables
The System
class also provides a method, called getenv()
, which returns all defined environmental variables:
EnvironmentalVariablesExample.java:
import java.util.Map; public class EnvironmentalVariablesExample { public static void main(String[] args) { Map vars = System.getenv(); // Print all defined environmental variables. for(String key: vars.keySet()) System.out.println("Key: " + key + ", Value: " + vars.get(key)); } }
A sample execution is shown below:
Key: PATH, Value: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games Key: LC_MEASUREMENT, Value: el_GR.UTF-8 ......... Key: USER, Value: stathis Key: HOME, Value: /home/stathis
Current time
The System
class contains a sample method, called currentTimeMillis()
, which returns the current time in milliseconds. The value returned by this method is defined as the difference between the current time and the midnight of January 1st, 1970 UTC.
A sample invocation is shown below:
System.out.println("The current time in milliseconds: " + System.currentTimeMillis());
You can download the full source code of this example here: SystemClassExample.zip.