Previous | Next | Trail Map | Writing Java Programs | Table of Contents


Using System Resources (1.1notes)

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 the System class include:

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's gc method. Also, you can force the runtime system to perform object finalization using System's runFinalization 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.

The Runtime Object

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, 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.


Previous | Next | Trail Map | Writing Java Programs | Table of Contents