|
Of course, we must be sure that variables have values before we attempt to print them.
|
In the statement
System.out.println("Hello");the string "Hello" contained within the parentheses is called the argument of the println method. The print and println methods both print strings. If we give an argument that is not a string, it must be converted to a string. For all of Java's primitive types, this conversion is done automatically.
It is also possible to use a mixture of strings and variables of other types as the argument of the print or println methods. In this case, Java will, again, automatically convert any non-string values to strings. Then the concatenation operator (+) can be used to produce one string for printing.
|
We must be careful when we print more than one variable with one print or println statement as the results may not be what we want.
|
As the previous examples illustrate, integer values are converted to strings of just the right length before printing. To print boolean variables, the strings "true" or "false" are generated. For char variables, a one character string is produced. For floating point variables, the values are normally converted to strings containing 8 digits for float values and 16 digits for double values. However, any trailing zeros after a decimal point will not be printed.
|
The process of reading values into memory is rather complicated in Java. To make reading simpler, we have included with this text (in Appendix A and on our web site) a set of methods that allows you to read values from the standard input device for your computer (likely the computer's keyboard), in a fairly simple way. The methods are contained in a class called In. (If you prefer to use Java's methods for reading values, instructions for their use can be found in Appendix B.)
The class In is not part of Java; you will not likely see it used outside this text. To use the methods contained in In, obtain a copy of the file In.java, copy it into the directory that you are using for your Java programs, and then compile it, just as you would compile any Java program, to produce a file called In.class. Now, to read a value of a particular type, simply write the name of the appropriate method in an assignment statement as shown in the next example.
|
Notice the use of parentheses with the getInt method. Any time that we use a method in Java, we must include parentheses, even if, as is the case here, the method has no argument. (We have already seen this in the use of
System.out.println();to print a blank line.)
Other methods in the class In include: getLong to read a long value, getChar to read a single character, getString to read a string, getFloat to read a float value, and getDouble to read a double value. They are all used in the same way.
The methods all assume that each input value is terminated by a newline. Thus, to use them at a keyboard, you must press the enter key after keying in the value. If incorrect input is entered, the methods that were expecting numeric input give a value of zero. If the user simply hits the enter key when entering a string, the method getString produces the value "", the empty string that contains no characters.
Now that we have methods for both reading and printing, we can write interactive programs, ones that ask or prompt the user for input, wait until the user responds to the prompt by providing some input, process this input in some way, and produce some output.
|
|