System Class in Java

Naveen AnamaneniUpdated · 4 min read

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

Declaration
public final class System

Methods of System

MethodDescription
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.inThe 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.

SystemInfo.java▶ Try it
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());
    }
}
Output
Java version: 25.0.5.0.3
OS: Mac OS X
User home: /Users/navika
Theme: light
HOME variable: /Users/navika
Separator length: 1

Explanation: 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()
MeasuresThe current wall-clock timeElapsed time from an arbitrary starting point
UnitMillisecondsNanoseconds
Can jumpYes, when the system clock is adjustedNo, it only moves forward
Use forTimestampsMeasuring how long something takes

Example: Timing Code and Copying Arrays

TimingCopy.java▶ Try it
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));
    }
}
Output
[91, 78, 85]
sum = 499999500000
took less than a second? true

Explanation: 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

  1. Easy: Print the Java vendor, the operating system architecture and the current working directory using java.vendor, os.arch and user.dir.
  2. Harder: Time how long it takes to build a string of 100,000 characters with += and with StringBuilder, using nanoTime.

Quick check: System class

4 questions
1Which method should you use to measure elapsed time?
2Where do system properties come from?
3What does System.getenv("MISSING") return?
4Can you create an object of System?

Key Takeaways

Was this tutorial helpful?

Lesson 336 of 482