Java System Methods Reference
›Short versionOpen if you only have a minute.
- System.out, System.err, and System.in are the process-wide standard streams.
- currentTimeMillis is wall-clock time since the Unix epoch. The clock can jump.
- nanoTime is for elapsed time. Its starting point is arbitrary, so a single reading is not a timestamp.
- arraycopy copies into an existing array. The order is source, source start, destination, destination start, count.
- getProperty reads JVM properties. getenv reads the process environment. They are different maps.
- exit ends the JVM. It does not return to the caller.
System exposes standard input, output, error streams, time helpers, properties, environment variables, array copying, and JVM termination hooks.
Methods
| API | Parameters | Returns | Behavior | Watch for |
|---|---|---|---|---|
| currentTimeMillis() | None. | long milliseconds since Unix epoch. | Reads wall-clock time. | Clock changes can affect elapsed-time measurements. |
| nanoTime() | None. | long monotonic-ish nanosecond value. | Useful for measuring elapsed durations. | Value has arbitrary origin; compare differences only. |
| arraycopy(src, srcPos, dest, destPos, length) | Source array, source start, destination array, destination start, count. | void. | Copies elements efficiently, including overlapping ranges. | Throws for null arrays, bad ranges, or incompatible types. |
| getProperty(String key) | System property name. | String value or null. | Reads JVM system properties. | Some properties may be absent or restricted by security policy. |
| getenv(String name) | Environment variable name. | String value or null. | Reads process environment variables. | Environment is typically immutable after process start. |
| exit(int status) | Status code, usually 0 for success and nonzero for failure. | Never returns normally. | Terminates the current JVM. | Can skip normal control flow and complicate tests. |
Which call fits
| Option | Best for | Tradeoff |
|---|---|---|
| currentTimeMillis() | Timestamps and human wall-clock time. | Not ideal for benchmarking elapsed durations. |
| nanoTime() | Measuring elapsed time within the same JVM process. | Not meaningful as an absolute timestamp. |
| Arrays.copyOf() | Creating a new resized copy. | Less flexible than arraycopy() for copying into an existing array. |
Examples
Measure elapsed time
java
long start = System.nanoTime();
doWork();
long elapsed = System.nanoTime() - start;
System.out.println("Elapsed ns: " + elapsed);Copy part of an array
java
int[] source = {1, 2, 3, 4};
int[] dest = new int[2];
System.arraycopy(source, 1, dest, 0, 2);
System.out.println(Arrays.toString(dest)); // [2, 3]A method calls System.exit(0) on a path you thought was an error. What runs after that call in the same method?
Nothing in that method, and nothing else in the JVM, except shutdown hooks. exit does not return. A finally block in the caller is not guaranteed to run, because the JVM is leaving. Return a status to main when the caller still needs to clean up.