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
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 theSystem
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
ClassAll ofSystem
's methods and variables are class methods and class variables. You don't instantiate theSystem
class to use it; you use theSystem
class's methods and variables directly from a reference to theSystem
class.The Standard I/O Streams
Probably the most often used items from theSystem
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
PropertiesProperties 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 theSystem
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 usingSystem
'sgc
method. Also, you can force the runtime system to perform object finalization usingSystem
'srunFinalization
method.Miscellaneous
System
MethodsTheSystem
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.The
Runtime
ObjectMost system programming needs are met through the programming interface provided by theSystem
class. However, in rare cases, a program must bypass the system-independent interface of theSystem
class and use system resources directly from the runtime environment. The Java environment provides aRuntime
object, which represents the current runtime environment. You can use aRuntime
object to access system resources directly.
Note: Messaging theRuntime
object directly compromises your ability to run your program on different systems. You should do this only in special situations.
Table of Contents |