System Class in Java
The System class in Java, in java.lang, is a final class with static members that connect your program to its surroundings. It is used to read the clock (currentTimeMillis, nanoTime), system properties such as the Java version and operating system, and environment variables, and it holds the standard streams System.out, System.err and System.in. You can't create a System object; you use its members directly. In this lesson, you will learn its most useful methods.
Declaration
public final class SystemMethods of System
| Method | Description |
|---|---|
long currentTimeMillis() | It is used to get the current time in milliseconds since 1 January 1970 UTC. |
long nanoTime() | It returns a high-precision counter for measuring elapsed time. |
String getProperty(String key) | It returns a system property, such as java.version or os.name. |
String getProperty(String key, String def) | It returns the property, or a default if it isn't set. |
String getenv(String name) | It returns an environment variable, such as PATH or HOME. |
String lineSeparator() | It returns the line separator of the operating system. |
void arraycopy(src, srcPos, dest, destPos, length) | It copies part of one array into another. |
int identityHashCode(Object o) | It returns the object's default hash code. |
void exit(int status) | It stops the JVM with a status code. |
System.out, System.err, System.in | The standard output, error and input streams. |
Example: Properties and Environment
Let's see an example that reads information about the running program. The values depend on the computer, so the output is a sample.
public class SystemInfo {
public static void main(String[] args) {
System.out.println("Java version: "
+ System.getProperty("java.version"));
System.out.println("OS: " + System.getProperty("os.name"));
System.out.println("User home: " + System.getProperty("user.home"));
System.out.println("Theme: "
+ System.getProperty("app.theme", "light"));
System.out.println("HOME variable: " + System.getenv("HOME"));
System.out.println("Separator length: "
+ System.lineSeparator().length());
}
}Java version: 25.0.5.0.3
OS: Mac OS X
User home: /Users/navika
Theme: light
HOME variable: /Users/navika
Separator length: 1Explanation: System properties are set by the JVM (and by -Dname=value on the command line). Environment variables come from the operating system. getProperty with a default returns "light" because app.theme wasn't set. The line separator is one character (\n) on macOS and Linux, and two (\r\n) on Windows.
currentTimeMillis vs nanoTime
currentTimeMillis() | nanoTime() | |
|---|---|---|
| Measures | The current wall-clock time | Elapsed time from an arbitrary starting point |
| Unit | Milliseconds | Nanoseconds |
| Can jump | Yes, when the system clock is adjusted | No, it only moves forward |
| Use for | Timestamps | Measuring how long something takes |
Example: Timing Code and Copying Arrays
import java.util.Arrays;
public class TimingCopy {
public static void main(String[] args) {
int[] marks = {91, 78, 85, 66, 72};
int[] topThree = new int[3];
System.arraycopy(marks, 0, topThree, 0, 3);
System.out.println(Arrays.toString(topThree));
long start = System.nanoTime();
long sum = 0;
for (int i = 0; i < 1_000_000; i++) {
sum += i;
}
long micros = (System.nanoTime() - start) / 1_000;
System.out.println("sum = " + sum);
System.out.println("took less than a second? " + (micros < 1_000_000));
}
}[91, 78, 85]
sum = 499999500000
took less than a second? trueExplanation: System.arraycopy(src, 0, dest, 0, 3) copies three elements from the start of marks into topThree. For timing, nanoTime is read before and after the work, and the difference is the elapsed time. (The exact time changes on every run, so the example only checks that it's under a second.)
Common Mistakes
Measuring elapsed time with currentTimeMillis. Clock adjustments can make it jump; use nanoTime.
Calling System.exit in library code. It stops the whole JVM; throw an exception instead.
Unboxing or using getenv results without a null check. Missing variables return null.
Practice
- Easy: Print the Java vendor, the operating system architecture and the current working directory using
java.vendor,os.archanduser.dir. - Harder: Time how long it takes to build a string of 100,000 characters with
+=and withStringBuilder, usingnanoTime.
Key Takeaways
Was this tutorial helpful?