Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

The System class

Reference for java.lang.System: standard streams, properties and environment, timing, array copying, line separators, garbage collection hints and exit.

System is a final utility class in java.lang that gives programs access to the environment they run in: the three standard streams, system properties, environment variables, clocks, and a few JVM-wide services. It cannot be instantiated; everything is static.

You will use it constantly without noticing: every System.out.println call goes through it. Most of its members have their own reference pages, linked below.

Java
System.out.println("Hello");
long start = System.nanoTime();
String os = System.getProperty("os.name");

Try it

Java A tour of System

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Replacing the standard output

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public final class System
Member Description
static PrintStream out Standard output. See System.out.
static PrintStream err Standard error.
static InputStream in Standard input. See System.in.
currentTimeMillis(), nanoTime() Clocks. See timing.
arraycopy(src, srcPos, dest, destPos, length) Fast array copy. See System.arraycopy().
getProperty(String), getProperty(String, String), getProperties(), setProperty System properties.
getenv(String), getenv() Environment variables (read-only). See properties and environment.
lineSeparator() The platform line separator (\n on Linux and macOS, \r\n on Windows).
exit(int status) Shuts down the JVM. See System.exit().
gc() A request to run garbage collection.
identityHashCode(Object) The hash Object.hashCode() would give, even if hashCode is overridden. Works with null (returns 0).
setIn, setOut, setErr Redirect the standard streams.
console() The Console, or null if none is available. Check isTerminal() (Java 22+) to know whether it is interactive.
getLogger(String) A System.Logger for platform logging (since Java 9).

Usage notes

  • Streams are global. setOut changes the stream for every thread in the JVM, so restore it in a finally block and avoid it in code that runs concurrently. Test frameworks often use it to capture output.
  • System.gc() is only a hint. The JVM may ignore it, and calling it routinely hurts performance. It is useful in experiments and some benchmarks, not in production code.
  • System.in, out and err are not closed for you. Closing System.in makes later reads fail; closing System.out silences the rest of the program.
  • System.err is unbuffered by convention and separate from out, so diagnostics stay visible when output is redirected to a file.
  • Security manager removed. The old System.getSecurityManager() and setSecurityManager() methods were deprecated for removal in Java 17 and permanently disabled in Java 24, so code should not rely on them.
  • runFinalization() is deprecated for removal along with finalizers; use Cleaner and try-with-resources instead.
  • Prefer lineSeparator() or %n over hard-coded "\n" when text will be read by other platforms' tools.
  • Properties are visible to everyone in the JVM. Do not store secrets in them.