Java System Class

Beginner
9 min read
Updated: Nov 2026
3 examples
java.lang.System

Definition and Usage

In Java, java.lang.System is the built-in class that connects your program to the world outside it: the console streams, the clock, the operating system's environment variables, Java's own configuration (system properties), native libraries, and the program's exit status.

The class is final and its constructor is private, so it cannot be instantiated: you never write new System(). Everything is static, and because it lives in java.lang, you never import it. Popular methods include arraycopy(), nanoTime(), getProperty(), getenv(), setOut(), and exit(). The System Methods index lists every guide.

Three static fields.

System.in (an InputStream), System.out, and System.err (both PrintStream) are the standard input, output, and error streams. They are final fields, but setIn(), setOut(), and setErr() can still replace them.

Java Syntax

Write the class name, a dot, and the field or method. No import needed:

Java
System.out.println("Hello");                 // standard output
System.err.println("Something went wrong");  // standard error
long now = System.currentTimeMillis();       // wall-clock milliseconds
String sep = System.lineSeparator();         // "\n" or "\r\n"
System.exit(1);                              // end with an error status

In System.out.println("Hello"), System is the class, out is its static PrintStream field, and println is a method of PrintStream, not of System.

The Three Standard Streams

FieldTypeUsed forReplace with
System.inInputStreamKeyboard input or piped datasetIn()
System.outPrintStreamNormal program outputsetOut()
System.errPrintStreamError and warning messagessetErr()

The shell can send out and err to different places, for example java App > out.txt 2> err.txt. For an interactive terminal with password input, see console() and the Java Console class.

System Properties vs Environment Variables

System propertyEnvironment variable
Belongs toThe JVMThe operating system process
Set byjava -Dapp.mode=test or setProperty()The shell before Java starts
Read withgetProperty(), getProperties()getenv()
Can Java change it?Yes (for this JVM only)No, read-only
Examplesjava.version, user.home, line.separatorPATH, HOME, JAVA_HOME

Measuring Time

currentTimeMillis() returns the wall-clock time in milliseconds since January 1, 1970 UTC. It is right for timestamps, but it can jump when the clock is adjusted. nanoTime() returns a timer value with an arbitrary starting point: meaningless alone, perfect for elapsed time: long start = System.nanoTime(); ... long took = System.nanoTime() - start;

Exit Codes

exit() stops the JVM and hands a status number to whoever started the program. 0 means success, any other value means an error. Check it with echo %ERRORLEVEL% in Windows Command Prompt, $LASTEXITCODE in PowerShell, or echo $? on Linux and macOS. When main simply ends, the status is 0.

The Security Manager Is Gone

Old Java code used setSecurityManager() and getSecurityManager() to sandbox untrusted code. Both were deprecated for removal in Java 17. Since Java 18, installing a manager at run time throws UnsupportedOperationException unless the JVM is started with -Djava.security.manager=allow, and newer releases (Java 24 and later) disable the feature completely. Modern code does not use it.

Java Examples

Example 1 — print, println, and printf on System.out

System.out is a PrintStream field of the System class. These are the three calls almost every Java program uses.

Java
public class SystemHubPrinting {
    public static void main(String[] args) {
        String item = "Notebook";
        int qty = 3;
        double price = 2.5;
        System.out.print("Item: ");
        System.out.println(item);
        System.out.println("Quantity: " + qty);
        System.out.printf("Total: %.2f%n", qty * price);
        System.out.println();
        System.out.println("Done");
    }
}

How It Works

  1. print stays on the line. System.out.print("Item: ") writes text without a line break, so the next println(item) finishes the same line: "Item: Notebook".
  2. printf formats numbers. %.2f shows 3 * 2.5 = 7.5 with two decimals as 7.50, and %n adds the platform line break (output shown for an English locale).
  3. An empty println. System.out.println() with no argument prints just a line break, which shows up as the blank line before "Done".

Example 2 — Read Settings from Properties and Environment Variables

A system property lives inside the JVM; an environment variable comes from the operating system. Both return null when the name is unknown.

Java
public class SystemHubSettings {
    public static void main(String[] args) {
        System.setProperty("shop.currency", "EUR");
        System.out.println("Currency: " + System.getProperty("shop.currency"));
        System.out.println("Tax rate: " + System.getProperty("shop.tax", "20"));
        System.out.println("Unknown property: " + System.getProperty("shop.unknown"));

        String region = System.getenv("CODETOFUN_SHOP_REGION_42");
        System.out.println("Region variable set? " + (region != null));
        System.out.println("Java version known? " + (System.getProperty("java.version") != null));
    }
}

How It Works

  1. A property you set yourself. setProperty("shop.currency", "EUR") stores the pair inside this JVM, so getProperty returns exactly EUR.
  2. Defaults and null. "shop.tax" was never set, so the two-argument getProperty returns the default "20"; the one-argument call for "shop.unknown" returns null.
  3. Only print safe facts. The made-up environment variable does not exist, so getenv gives null; the standard "java.version" property always exists, but its value differs per computer, so only the check is printed.

Example 3 — Copy an Array and Time the Work

System.arraycopy copies a range of elements, and System.nanoTime measures how long it took. Only a true/false check about the time is printed, because the duration differs on every run.

Java
import java.util.Arrays;

public class SystemHubCopyTime {
    public static void main(String[] args) {
        int[] week = {5, 8, 6, 9, 7, 4, 3};
        int[] weekdays = new int[5];

        long start = System.nanoTime();
        System.arraycopy(week, 0, weekdays, 0, 5);
        long elapsed = System.nanoTime() - start;

        System.out.println("Weekdays: " + Arrays.toString(weekdays));
        System.out.println("Elapsed time measured: " + (elapsed >= 0));
    }
}

How It Works

  1. Copy the first five. arraycopy(week, 0, weekdays, 0, 5) copies elements 0 to 4 of week into positions 0 to 4 of weekdays.
  2. Two timer readings. nanoTime() is read before and after the copy; the difference is the elapsed time in nanoseconds.
  3. Print a fact, not a number. The exact nanoseconds change every run, so the program prints only elapsed >= 0, which is true.

Next: Java System Methods by Group

Full list with short descriptions: Java System Methods index.

Frequently Asked Questions

java.lang.System is a final class that cannot be instantiated. Its static fields in, out, and err are the standard streams, and its static methods copy arrays, read the clock, read and change system properties, read environment variables, load native libraries, and end the program.
No. Every class in java.lang is imported automatically, so System.out.println works in any Java file.
A system property is a key and value pair inside the JVM, set with -Dname=value on the command line or with System.setProperty. An environment variable belongs to the operating system process, is set before Java starts, and can only be read with System.getenv.
Use nanoTime() for elapsed time, because it does not jump when the computer clock is changed. Use currentTimeMillis() when you need the actual date and time as milliseconds since January 1, 1970 UTC.
It stops the JVM and reports status 0, which means success to the shell or script that started the program. A nonzero status such as 1 means an error. finally blocks after the call do not run, but shutdown hooks do.

Browse every System method

Open the methods index for categorized guides with examples and FAQs.

System Methods Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful