Java Reference
In-Depth Information
Chapter Summary
•
You can use
System.out.println
for simple console output.
•
You can use
System.out.printf
for formatted console output.
•
You can use
NumberFormat.getCurrencyInstance()
to produce an object that
can convert numbers to strings that show the number as a correctly formatted
currency amount, for example, by adding a dollar sign and having exactly two
digits after the decimal point.
•
You can use the class
DecimalFormat
to output numbers using almost any format
you desire.
•
You can use an object of the class
Scanner
for reading keyboard input.
Answers to Self-Test Exercises
1.
HelloJoe
2.
System.out.println("One two buckle your shoe.");
System.out.println("Three four shut the door.");
3.
System.out.println
ends a line of input, so the next output goes on the next
line.
With
System.out.print
, the next output goes on the same line.
4.
2 2
4
Note that
2 + " " + 2
contains a string, namely
" "
. So, Java knows it is supposed to produce a string.
On the other hand,
2 + 2
contains only integers. So Java thinks
+
denotes
addition in this second case and produces the value
4
to be output.
5.
START1234567890
STARTJelly beansEND
STARTJelly beansEND
START Jelly beansEND
6.
The last two of the following lines end with a blank. The last line begins with
two blanks, one that follows
%n
in the format string and one because the field
width is
9
but the number to be output fills only eight spaces.
START1234567890
STARTJelly beansEND
123.1235
7.
System.out.printf("%15.3e", d);
8.
$1.23
$15.68