System
About
The System
class in Java, part of the java.lang
package, provides access to various system-level resources and functionalities. It is a utility class that offers methods for input, output, and other system-related tasks like working with properties, environment variables, and performing garbage collection. The class cannot be instantiated because it has a private constructor.
The System
class is commonly used for tasks such as getting the current time, accessing environment variables, reading from standard input, writing to standard output, and controlling the system's exit process.
Features
Utility Methods: Provides utility methods for handling standard input/output and other system properties.
System Properties: Accesses system properties like file paths, user details, and Java environment variables.
Environment Variables: Allows access to environment variables set in the operating system.
Time Measurement: Provides methods like
currentTimeMillis()
andnanoTime()
for measuring time intervals.Garbage Collection: Offers methods to suggest the JVM to perform garbage collection.
I/O Streams: Provides access to standard input, output, and error streams (
System.in
,System.out
,System.err
).Security Management: Allows interaction with the security manager in Java to control permissions and access to system resources.
Internal Working
The System
class is designed to interact with the Java runtime environment and the underlying operating system. Here's an overview of its internal workings:
Private Constructor:
The
System
class has a private constructor, preventing instantiation. This enforces that its utility methods are used statically.
System Properties:
System properties are key-value pairs that describe the environment in which the Java application is running. The
System
class provides methods likegetProperty()
andsetProperty()
to manage these properties.These properties can represent various details such as file paths (
user.dir
), OS name (os.name
), Java version (java.version
), etc.
I/O Streams:
The class provides access to standard input (
System.in
), output (System.out
), and error (System.err
) streams, which are used to read from and write to the console.
Environment Variables:
Environment variables are values provided by the operating system. The
System
class exposes methods likegetenv()
to access environment variables, allowing Java programs to interact with the system environment.
Time Methods:
System.currentTimeMillis()
provides the current time in milliseconds since the Unix epoch (January 1, 1970).System.nanoTime()
returns the current value of the most precise available system timer in nanoseconds. This is useful for measuring elapsed time.
Exit and Security:
The
System.exit()
method allows you to terminate the JVM with a specified exit status. A non-zero exit code typically indicates an error.System.setSecurityManager()
allows setting a custom security manager to manage security policies for the running application.
GC Control:
The
System.gc()
method suggests that the JVM should perform garbage collection. However, it is not guaranteed that the garbage collection will occur immediately.
Key Methods
Method
Description
currentTimeMillis()
Returns the current time in milliseconds since January 1, 1970 (the Unix epoch).
nanoTime()
Returns the current value of the most precise available system timer in nanoseconds. Useful for measuring elapsed time.
exit(int status)
Exits the Java application. A non-zero status code generally indicates an abnormal termination.
getProperty(String key)
Retrieves the value of a system property specified by the key.
setProperty(String key, String value)
Sets the system property with the specified key and value.
getenv()
Retrieves environment variables as a map of key-value pairs.
getenv(String name)
Retrieves a specific environment variable by name.
lineSeparator()
Returns the system's line separator ( for UNIX, \r
for Windows).
arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
Copies an array from the source to the destination array.
setSecurityManager(SecurityManager sm)
Sets the security manager for the Java runtime environment.
gc()
Suggests that the JVM performs garbage collection (not guaranteed).
in
The standard input stream (System.in) for reading data from the console.
out
The standard output stream (System.out) for printing data to the console.
err
The standard error stream (System.err) for printing error messages to the console.
Limitations
No Instantiation: The
System
class cannot be instantiated due to its private constructor.Platform Dependent: Some of the functionality provided by the
System
class is dependent on the underlying operating system, like environment variables and file path structures.No Direct Access to Hardware: While the
System
class can interact with the OS, it does not provide direct access to hardware components like CPU or memory in a detailed manner. For such functionality, you would need to use Java libraries or native code.Unreliable
System.gc()
: TheSystem.gc()
method is merely a suggestion to the JVM to run garbage collection, and it does not guarantee that GC will occur.Security: The
System
class has access to the environment, which could lead to security concerns if misused, especially with methods likesetSecurityManager()
.
Real-World Usage
Measuring Execution Time: The
System.nanoTime()
andSystem.currentTimeMillis()
methods are commonly used in performance benchmarking to measure elapsed time between operations.Accessing Environment Variables: The
System.getenv()
andSystem.getProperty()
methods are used to fetch environment variables and system properties, which are helpful for configuration management, such as reading Java version or system-specific settings.Program Termination: The
System.exit()
method is used in cases where a program needs to be terminated with a specific exit status, often in command-line tools or when handling fatal errors.Standard I/O Operations: The
System.out
andSystem.err
streams are widely used for printing messages to the console.System.in
is used for reading user input in command-line applications.Managing Line Separator: The
System.lineSeparator()
is useful for generating platform-independent line breaks in text files, making Java programs more portable across different operating systems.
Examples
1. Measuring Elapsed Time Using nanoTime()
nanoTime()
2. Accessing System Properties
3. Using System.exit()
System.exit()
4. Reading Environment Variables
5. Accessing Standard Input, Output, and Error
Last updated
Was this helpful?