Chapter 3. Input and Output

The programs you’ve looked at so far simply display messages, which doesn’t really involve that much computation. This chapter shows you how to read input from the keyboard, use that input to calculate a result, and then format that result for output.

The System Class

We have been using System.out.println for a while, but you might not have thought about what it means. System is a class that provides methods related to the system, or environment. where programs run. It also provides System.out, which is a special value that has additional methods (like println) for displaying output.

In fact, we can use System.out.println to display the value of System.out:

System.out.println(System.out);

The result is shown here:

java.io.PrintStream@685d72cd

This output indicates that System.out is a PrintStream, which is defined in a package called java.io. A package is a collection of related classes; java.io contains classes for I/O which stands for input and output.

The numbers and letters after the @ sign are the address of System.out, represented as a hexadecimal (base 16) number. The address of a value is its location in the computer’s memory, which might be different on different computers. In this example, the address is 685d72cd, but if you run the same code, you will likely get something else.

As shown in Figure 3-1, System is defined in a file called System.java, and PrintStream is defined in PrintStream.java. These files are part of the Java library, which ...

Get Think Java, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.