Java Programs  «Prev  Next»
Lesson 6Communicating with standard I/O
ObjectiveExplain Java's standard approach for inputting and outputting text.

Communicating with Standard I/O

The easiest way to input and output information in Java is to use standard I/O, which is the most basic form of input and output available to a Java program.
Standard I/O in Java is text-based, and operates under the premise that the computer has a standard input device and a standard output device, which are typically the keyboard and monitor.
Using standard I/O, you can input information from the keyboard and output it to the monitor with very little code using a couple of special Java objects.
These objects are part of the standard System object. The in and out standard I/O objects are located within the System object and are programmatically referred to as System.in and System.out.

System.in object

The System.in object is used to retrieve text input from the keyboard. It is primarily used in command-line applications since graphical applications typically provide a graphical user interface to obtain input from the user.
Command-line arguments, however, are often used instead of the System.in object. In most cases you will find that command-line arguments provide more flexibility than the System.in object when it comes to passing information to a command-line application.

System.out Object

The System.out object is used to output text data to the monitor. It contains a method called println() that is used to print a line of text to the standard output device (monitor). Following is an example of using the println() method to print a simple text message:
System.out.println("Howdy pardner!");
The System.out object also has a print() method that is very similar to the println() method.
The difference between the two is that the println() method starts a new line after it is finished printing the text.

Standard Java - Exercise

Develop the HelloWorld application in this exercise by clicking the link below.
Standard Java - Exercise