|
|
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 theSystemclass 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
SystemClassAll ofSystem's methods and variables are class methods and class variables. You don't instantiate theSystemclass to use it; you use theSystemclass's methods and variables directly from a reference to theSystemclass.The Standard I/O Streams
Probably the most often used items from theSystemclass 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 theSystemclass.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 usingSystem'sgcmethod. Also, you can force the runtime system to perform object finalization usingSystem'srunFinalizationmethod.Miscellaneous
SystemMethodsTheSystemclass includes several miscellaneous methods that let you get the current time in milliseconds, exit the interpreter, copy arrays, and work with the security manager.The
RuntimeObjectMost system programming needs are met through the programming interface provided by theSystemclass. However, in rare cases, a program must bypass the system-independent interface of theSystemclass and use system resources directly from the runtime environment. The Java environment provides aRuntimeobject, which represents the current runtime environment. You can use a
Runtimeobject to access system resources directly.
Note: Messaging theRuntimeobject directly compromises your ability to run your program on different systems. You should do this only in special situations.
|
|
Table of Contents |