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 the Java Programmer's Guide, you've no doubt already seen the System class's standard output stream used by several examples to display text. Other system resources available through the System class include:
- standard input, output and error streams
- system properties
- garbage collection
- loading dynamic libraries
- and 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 method and class variables. That is, you don't instantiate the System class to use it; you use the class directly.Standard Output and Error Streams
Probably the most often used items from the System class are the streams used for writing text. System provides two streams for writing text--the standard output and standard error streams.Standard Input Stream
In addition to the two output streams, System provides a stream for reading text--the standard input stream. The example program in The Nuts and Bolts of the Java Language uses the standard input stream to count the number of characters typed in by the user. Visit The Standard Input Stream in that lesson for a discussion about the standard input stream.System Properties
[PENDING: this page is under construction]Garbage Collection
[PENDING: this page is under construction]Loading Dynamic Libraries
[PENDING: this page is under construction]Miscellaneous System Methods
The System class includes several miscellaneous methods that let you get the current time in milliseconds (currentTimeMillis()
), exit the interpreter (exit()
), copy arrays (arrayCopy()
) and work with the security manager (getSecurityManager()
andsetSecurityManager
). This page currently only contains information about thecurrentTimeMillis()
andexit()
methods.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 that you can use to access system resources directly.
Note: messaging the Runtime object directly compromises your ability to run your program on different systems. You should only do this in special situations.
Table of Contents |