Section 3b

Standard I/O in Java


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The Fundamentals of Input and Output

Connecting to a stream. Generally speaking, computer systems tend to treat every component connected to it as a component or a device with objects on or in them that can be opened or closed like data files. This is inherited from the operating system days of the language, and it means that the keyboard, the monitor, the hard drive, your memory sticks and CD or DVD recorder/players are all considered to act like devices managing computer files. The data quantities on or in these devices can be opened or closed, and things can be sent (or written) to them, and/or things can be received (or read) from them.

The metaphor is used that assumes that all the devices are connected to a flowing stream of data inside the computer, called (amazingly enough) a data stream. So if you want to get data from the stream, you must open the device like a file, and either send data to it through the stream, or get data from it through the stream.

For purposes of what are called console operations, meaning operations with console devices called keyboards and monitors, Java assumes that the standard input and output (I/O) devices, like the keyboard and monitor have already been opened and are prepared for reading/inputting, or writing/outputting. This isn't as purely true as it has been in earlier languages but it is helpful to think of these devices, and the computer interactions, this way.

Doing output. The "file" access point for the console output device (i.e., the computer monitor) is System.out., which is essentially the standard output stream for Java operations, and to move data to the monitor, you must insert data into the stream using System.in. In reality, these metaphorical "file" access points are really objects of classes that can manage the output actions for your programs. The standard operation for output in Java is shown here:

System.out.print( "This is an output statement" );

The text data is inserted or sent toward the output device by this operation, and for output, it is common to tell the monitor window to end the line after you have sent some data. The above action simply places text on the screen. One can also use the method:

System.out.println( "This is an output statement" );

The very small change of adding ln to the end of the print identifier object supports ending the line after the statement has been printed. Note that you only use quotes on text that will be output. The print method allows for concatenation (or combining) of items into a string using plus signs. For example, if you had a variable that held a student's age, a print statement might look like the following.

System.out.println( "You are " + age + " years old" );

Note the spaces placed in the text before and after the age variable; these are necessary so that your text is not jammed up against your output display. Not having the spaces after "are" and before "years" would make the above output be presented as shown here:

Image of jammed together text

It is important to consider what your output would look like, and you should check this regularly.

Doing input. While Java has an amazing number of ways to capture input using Graphical User Interface (GUI) operations with a variety of windows, widgets (e.g., check boxes, radio buttons, etc.), and mouse input, it is not a great language for conducting command line input. As mentioned previously System.in and related Java classes can handle these operations, and in fact, will be the foundation of the I/O operations you will be using in this reference. However, they are not as friendly as System.out operations. Use of the System.in operations will be left for later analysis. For the time being, enjoy using the much friendlier tool Console_IO_Class found in the next section.