Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
System.in.read(). It turns out that System.in.read() inputs data from the standard input device. To demonstrate outputting data, examples called System.out.print() and System.out.println(). In contrast to System.in.read(), those methods -- named sequences of executable code (to be explored in next month's article) -- send their output to the standard output
device. Want to know more about standard I/O concepts? Read on!Standard I/O is a standardized input/output mechanism that originates from the Unix operating system. Although this mechanism is mostly used with older non-GUI operating systems, standard I/O still plays a role in modern GUI (graphical user interface) operating systems, where people use it to debug malfunctioning programs and to teach input/output in entry-level programming courses.
As you've probably guessed, standard I/O uses devices for inputting and outputting data. These devices include standard input, standard output, and standard error.
The standard input device is that part of the operating system that controls from where a program receives its input. By default, the standard input device reads that input from a device driver attached to the keyboard. However, you can redirect, or switch, the input source to a device driver attached to a file so that input seems to be "magically" coming from a file -- instead of the keyboard.
A program inputs its data from the standard input device by calling Java's System.in.read() method. Look in the SDK documentation and you'll discover a class called System. That class contains a variable called in -- an object created from a subclass of InputStream. The period character after System states that in belongs to System, and the period character after in states that read() belongs to in. In other words, read() is a method that belongs to an object called in, which in turn belongs to a class called System. (I will discuss more about classes, objects, and "belonging to" next month.)
System.in.read() takes no arguments and returns an integer, which has led some to believe that System.in.read() returns user-entered integer numbers. To clarify, System.in.read() either returns a key's 7-bit ASCII code (if the standard input device is set to the keyboard) or an 8-bit byte from a file
(if the standard input device has been redirected from the keyboard to a file). In either case, System.in.read() converts the code to a 32-bit integer and returns the result.
Assume that the standard input device is set to the keyboard. The following is a description of what happens under Windows:
When you type a key on a Windows-controlled keyboard, the operating system stores that key's 7-bit ASCII code in an internal
key buffer. That key buffer holds up to roughly 16 ASCII codes and is organized as a first-in/first-out circular queue data
structure. System.in.read() retrieves the ASCII code from the head of the key buffer and then removes that code from the key buffer. That 7-bit ASCII
code then converts to an int -- by System.in.read() prepending 25 zero bits to the code -- and returns to the method's caller. A second System.in.read() method call retrieves the next ASCII code, which is now at the head of the key buffer, and so on.