The System class is a final class that exposes useful static fields and static methods that can help you with common tasks.
The three fields of the System class are out, in, and err:
public static final java.io.PrintStream out;
public static final java.io.InputStream in;
public static final java.io.PrintStream err;
The out field represents the standard output stream which by default is the same console used to run the running Java application. You will learn more about PrintStream in later sections, but for now know that you can use the out field to write messages to the console. You will often write the following line of code:
System.out.print(
message
);
where message is a String object. However, PrintStream has many print method overloads that accept different types, so you can pass any primitive type to the print method:
System.out.print(12);
System.out.print('g');
In addition, there are println methods that are equivalent to print, except that println adds a line terminator at the end of the argument.
Note also that because out is static, you can access it by using this notation: System.out, which returns a java.io.PrintStream object. You can then access the many methods on the PrintStream object as you would methods of other objects: System.out.print, System.out.format, etc.
The err field also represents a PrintStream object, and by default the output is channeled to the console from where the current Java program was invoked. However, its purpose is to display error messages that should get immediate attention of the user.
For example, here is how you can use err:
System.err.println("You have a runtime error.");
The in field represents the standard input stream. You can use it to accept keyboard input. For example, the getUserInput method in Listing 5.2 accepts the user input and returns it as a String:
Listing 5.2: The InputDemo class
package app05;
import java.io.IOException;
public class InputDemo {
public String getUserInput() {
StringBuilder sb = new StringBuilder();
try {
char c = (char) System.in.read();
while (c != '\r' && c != '\n') {
sb.append(c);
c = (char) System.in.read();
}
} catch (IOException e) {
}
return sb.toString();
}
public static void main(String[] args) {
InputDemo demo = new InputDemo();
String input = demo.getUserInput();
System.out.println(input);
}
}
However, an easier way to receive keyboard input is to use the java.util.Scanner class.
The System class has many useful methods, all of which are static. Some of the more important ones are listed here.
public static void arraycopy
(Object
source
, int
sourcePos
,
Object
destination
, int
destPos
, int
length
)
This method copies the content of an array (source) to another array (destination), beginning at the specified position, to the specified position of the destination array. For example, the following code uses arraycopy to copy the contents of array1 to array2.
int[] array1 = {1, 2, 3, 4};
int[] array2 = new int[array1.length];
System.arraycopy(array1, 0, array2, 0, array1.length);
public static void exit
(int
status
)
Terminates the running program and the current JVM. You normally pass 0 to indicate that a normal exit and a nonzero to indicate there has been an error in the program prior to calling this method.
public static long currentTimeMillis
()
Returns the computer time in milliseconds. The value represents the number of milliseconds that has elapsed since January 1, 1970 UTC.
Prior to Java 8, currentTimeMillis was used to time an operation. In Java 8 and later, you can use the java.time.Instant class, instead.
public static long nanoTime
()
This method is similar to currentTimeMillis, but with nanosecond precision.
public static String getProperty
(String
key
)
This method returns the value of the specified property. It returns null if the specified property does not exist. There are system properties and there are user-defined properties. When a Java program runs, the JVM provides values that may be used by the program as properties.
Each property comes as a key/value pair. For example, the os.name system property provides the name of the operating system running the JVM. Also, the directory name from which the application was invoked is provided by the JVM as a property named user.dir. To get the value of the user.dir property, you use:
System.getProperty("user.dir");
Table 5.2 lists the system properties.
System property |
Description |
java.version |
Java Runtime Environment version |
java.vendor |
Java Runtime Environment vendor |
java.vendor.url |
Java vendor URL |
java.home |
Java installation directory |
java.vm.specification.version |
Java Virtual Machine specification version |
java.vm.specification.vendor |
Java Virtual Machine specification vendor |
java.vm.specification.name |
Java Virtual Machine specification name |
java.vm.version |
Java Virtual Machine implementation version |
java.vm.vendor |
Java Virtual Machine implementation vendor |
java.vm.name |
Java Virtual Machine implementation name |
java.specification.version |
Java Runtime Environment specification version |
java.specification.vendor |
Java Runtime Environment specification vendor |
java.specification.name |
Java Runtime Environment specification name |
java.class.version |
Java class format version number |
java.class.path |
Java class path |
java.library.path |
List of paths to search when loading libraries |
java.io.tmpdir |
Default temp file path |
java.compiler |
Name of JIT compiler to use |
java.ext.dirs |
Path of extension directory or directories |
os.name |
Operating system name |
os.arch |
Operating system architecture |
os.version |
Operating system version |
file.separator |
File separator (“/” on UNIX) |
path.separator |
Path separator (“:” on UNIX) |
line.separator |
Line separator (“\n” on UNIX) |
user.name |
User’s account name |
user.home |
User’s home directory |
user.dir |
User’s current working directory |
Table 5.2: Java system properties
public static void setProperty
(String
property
, String
newValue
)
You use setProperty to create a user-defined property or change the value of the current property. For instance, you can use this code to create a property named password:
System.setProperty("password", "tarzan");
And, you can retrieve it by using getProperty:
System.getProperty("password")
For instance, here is how you change the user.name property.
System.setProperty("user.name", "tarzan");
public static String getProperty(String
key
, String
default
)
This method is similar to the single argument getProperty method, but returns a default value if the specified property does not exist.
public static java.util.Properties getProperties
()
This method returns all system properties. The return value is a java.util.Properties object. The Properties class is a subclass of java.util.Hashtable.
For example, the following code uses the list method of the Properties class to iterate and display all system properties on the console.
java.util.Properties properties = System.getProperties();
properties.list(System.out);