Table of Contents |
Often, a program requires access to system resources such as properties, standard input and output streams, or the current time. Your program could use these resources directly from the current runtime environment, but your program would only be able to run in the environment for which it was written. Each time you wanted to run the program in a new environment, you would have to "port" the program by rewriting the system-dependent sections of code.The Java development environment solves this problem by allowing your program to use system resources through a system-independent programming interface implemented by the System class (a member of the java.lang package).
As you can see from this diagram, the System class allows your Java programs to use system resources but insulates them from system-specific details.
If you've experimented with other lessons in this tutorial, you've no doubt already seen the System class's standard output stream, which several examples use to display text. The system resources available through the System class include:
- standard input, output, and error streams
- system properties
- garbage collection
- loading dynamic libraries
- miscellany, including copying arrays, getting the current time, exiting the runtime environment, and using the security manager
Using the System Class
All of System's methods and variables are class methods and class variables. You don't instantiate the System class to use it; you use the System class's methods and variables directly from a reference to the System class.The Standard I/O Streams
Probably the most often used items from the System class are the streams used for reading and writing text. System provides one stream for reading text, the standard input stream, and two streams for writing text, the standard output and standard error streams.System Properties
Properties are key/value pairs that your Java programs can use to set up various attributes or parameters between invocations. The Java environment itself maintains a set of system properties that contain information about the current environment. You can access the system properties through the System class.Forcing Finalization and Garbage Collection
In Java, you don't have to free memory when you're done with it. The garbage collector runs asynchronously in the background cleaning up unreferenced objects. However, you can force the garbage collector to run using System'sgc()
method. Also, you can force the runtime system to perform object finalization using System'srunFinalization()
method.Miscellaneous System Methods
The System class includes several miscellaneous methods that let you get the current time in milliseconds, exit the interpreter, copy arrays, and work with the security manager.Using System-Dependent Resources
Most system programming needs are met through the programming interface provided by the System class. However, in rare cases, a program must bypass the system-independent interface of the System class and use system resources directly from the runtime environment. The Java environment provides a Runtime object (another member of the java.lang package), which represents the current runtime environment. You can use a Runtime object to access system resources directly.Note: Messaging the Runtime object directly compromises your ability to run your program on different systems. You should do this only in special situations.
Table of Contents |