The System class is a final utility class available in the java.lang package. It provides access to standard input, output, and error streams, system properties, environment variables, memory management utilities, and various JVM-related operations. Since all its members are static, objects of the System class are not created.
- Provides standard input (System.in), output (System.out), and error (System.err) streams.
- Contains utility methods for array copying, time measurement, environment access, and JVM control.
- Offers access to system properties and environment variables
Important Fields of System Class
| Field | Description |
|---|---|
| System.in | Standard input stream (keyboard input). |
| System.out | Standard output stream (console output). |
| System.err | Standard error stream for displaying errors. |
Types of System Class Methods
The methods of the System class can be grouped into different categories based on their functionality.
- Array Utility Methods: Used for copying array elements efficiently.
- System Property Methods: Used to get, set, and remove system properties.
- Time Utility Methods: Used for measuring execution time and obtaining current system time.
- Environment Variable Methods: Used to access operating system environment variables.
- JVM Control Methods: Used for garbage collection, JVM termination, and finalization.
- Stream Management Methods: Used to redirect input, output, and error streams.
- Native Library Methods: Used to load native libraries and map library names.
- Security Methods: Used to manage JVM security settings.
Commonly Used Methods of System Class
1. arraycopy()
The arraycopy() method is used to copy elements from one array to another efficiently. It is faster than manually copying elements using loops.
Syntax:
System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] source = {1, 2, 3, 4, 5};
int[] target = {10, 20, 30, 40, 50};
System.arraycopy(source, 0, target, 2, 2);
System.out.println(Arrays.toString(target));
}
}
Output
[10, 20, 1, 2, 50]
2. currentTimeMillis()
The currentTimeMillis() method returns the current time in milliseconds. It is commonly used for measuring execution time and timestamps.
Syntax
long time = System.currentTimeMillis();
public class Main {
public static void main(String[] args) {
long time = System.currentTimeMillis();
System.out.println(time);
}
}
Output
1781169523689
3. nanoTime()
The nanoTime() method returns the current value of the JVM's high-resolution timer in nanoseconds. It is useful for precise performance measurements.
Syntax:
long time = System.nanoTime();
public class Main {
public static void main(String[] args) {
long start = System.nanoTime();
for(int i = 0; i < 1000; i++);
long end = System.nanoTime();
System.out.println("Time Taken: " + (end - start));
}
}
Output
Time Taken: 5940
4. getProperty()
The getProperty() method retrieves a system property using its key. It provides information about the operating system, Java version, user directory, and more.
Syntax:
String value = System.getProperty(String key);
public class Main {
public static void main(String[] args) {
System.out.println(
System.getProperty("java.version")
);
}
}
Output
26-ea
5. setProperty()
The setProperty() method sets or updates a system property. It returns the previous value associated with the specified key.
Syntax:
System.setProperty(String key, String value);
public class Main {
public static void main(String[] args) {
System.setProperty("country", "India");
System.out.println(
System.getProperty("country")
);
}
}
Output
India
6. clearProperty()
The clearProperty() method removes a system property identified by the given key. After removal, the property value becomes null.
Syntax:
System.clearProperty(String key);
public class Main {
public static void main(String[] args) {
System.setProperty("city", "Delhi");
System.clearProperty("city");
System.out.println(
System.getProperty("city")
);
}
}
Output
null
7. getenv()
The getenv() method returns environment variables of the operating system. It is commonly used to access PATH, JAVA_HOME, and other environment settings.
Syntax:
System.getenv();
public class Main {
public static void main(String[] args) {
System.out.println(
System.getenv("PATH")
);
}
}
Output
/usr/java/openjdk-26/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
8. gc()
The gc() method requests the JVM to perform garbage collection. It helps reclaim memory occupied by unused objects.
Syntax:
System.gc();
public class Main {
public static void main(String[] args) {
System.gc();
System.out.println(
"Garbage Collection Requested"
);
}
}
Output
Garbage Collection Requested
9. exit()
The exit() method terminates the currently running Java Virtual Machine (JVM). A status code of 0 indicates successful termination.
Syntax:
System.exit(int status);
public class Main {
public static void main(String[] args) {
System.out.println("Program Ends");
System.exit(0);
System.out.println("This will not execute");
}
}
Output
Program Ends
10. lineSeparator()
The lineSeparator() method returns the platform-dependent line separator. It is useful when writing platform-independent applications.
Syntax:
String separator = System.lineSeparator();
public class Main {
public static void main(String[] args) {
String ls = System.lineSeparator();
System.out.print("Java");
System.out.print(ls);
System.out.print("Programming");
}
}
Output
Java Programming
11. identityHashCode()
The identityHashCode() method returns the default hash code of an object regardless of whether the class overrides hashCode().
Syntax:
System.identityHashCode(Object obj);
public class Main {
public static void main(String[] args) {
String str = "Java";
System.out.println(
System.identityHashCode(str)
);
}
}
Output
724542711
12. console()
The console() method returns the system console associated with the JVM. It is mainly used for secure user input.
Syntax:
Console c = System.console();
import java.io.Console;
public class Main {
public static void main(String[] args) {
Console c = System.console();
if(c != null) {
System.out.println("Console Available");
} else {
System.out.println("No Console Available");
}
}
}
Output
No Console Available
Advantages of System Class
- Provides direct access to standard input, output, and error streams.
- Offers utility methods for array copying and time measurement.
- Helps manage system properties and environment variables.
- Supports memory management through garbage collection.
- Allows JVM control using methods like exit().
- Provides platform-independent utilities such as lineSeparator().