|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.
As you can see from this diagram, the
Systemclass 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
Systemclass's standard output stream, which several examples use to display text. The system resources available through the
- 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
System's methods and variables are class methods and class variables. You don't instantiate the
Systemclass to use it; you use the
Systemclass's methods and variables directly from a reference to the
The Standard I/O StreamsProbably the most often used items from the
Systemclass are the streams used for reading and writing text.
Systemprovides one stream for reading text, the standard input stream, and two streams for writing text, the standard output and standard error streams.
SystemPropertiesProperties 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
Forcing Finalization and Garbage CollectionIn 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
gcmethod. Also, you can force the runtime system to perform object finalization using
Systemclass includes several miscellaneous methods that let you get the current time in milliseconds, exit the interpreter, copy arrays, and work with the security manager.
RuntimeObjectMost system programming needs are met through the programming interface provided by the
Systemclass. However, in rare cases, a program must bypass the system-independent interface of the
Systemclass and use system resources directly from the runtime environment. The Java environment provides a
Runtimeobject, which represents the current runtime environment. You can use a
Runtimeobject to access system resources directly.
Note: Messaging the
Runtimeobject directly compromises your ability to run your program on different systems. You should do this only in special situations.
|Table of Contents|