System.in.read() retrieves the ASCII code from the head of the key buffer and then removes that code from the 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. 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. In either case, System.in.read() converts the code to a 32-bit integer and returns the result.Īssume that the standard input device is set to the keyboard. 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). 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. (I will discuss more about classes, objects, and "belonging to" next month.) In other words, read() is a method that belongs to an object called in, which in turn belongs to a class called System. The period character after System states that in belongs to System, and the period character after in states that read() belongs to in. That class contains a variable called in - an object created from a subclass of InputStream. Look in the SDK documentation and you'll discover a class called System. 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.Ī program inputs its data from the standard input device by calling Java's System.in.read() method. By default, the standard input device reads that input from a device driver attached to the keyboard. The standard input device is that part of the operating system that controls from where a program receives its input. These devices include standard input, standard output, and standard error. 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.Īs you've probably guessed, standard I/O uses devices for inputting and outputting data. Standard I/O is a standardized input/output mechanism that originates from the Unix operating system. Want to know more about standard I/O concepts? Read on! 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. To demonstrate outputting data, examples called () and (). It turns out that System.in.read() inputs data from the standard input device. To demonstrate inputting data, several examples called System.in.read(). In prior Java 101 articles, I referred to the concepts of redirection, standard input device, and standard output device.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |