Java Reference
In-Depth Information
from scientists who refer to large numbers in scientific notation. For instance,
1,200,000 can be notated as 1.2 times 10 to the power of 6. The decimal point
floated six places to the left.
Performing Math with Floats and Doubles
Programmers must pay particular attention to data types when
multiplying or dividing numbers with decimal places. A rule of
arithmetic states that when two numbers are multiplied, the
number of decimal places in the answer equals the sum of each
number's decimal places. For example, if two numbers with four
decimal places each are multiplied together, the answer has eight
decimal places. Thus, while you might declare the numbers as
floats because they have only four decimal places, you would have
to declare the answer as double because it has eight decimal
places. Another example is that literal data not specified with an L
or F is considered double by the Java compiler. Therefore, you
must declare the result of any mathematical operation with
a nonspecified literal value as a double. If you try to store the
answer in a float location, you will receive an error that states,
possible loss of precision found, and the program will not compile.
User Input
Programmers who want to use timely data or data that changes on a regular
basis usually do not code literal data into their programs. Instead, programmers
code programs to reference an external data source, such as data input by a user.
For example, in the banking industry, a programmer must rely on a bank cus-
tomer to enter data such as a personal identification number (PIN) using the
ATM keyboard. In fact, it is more common to rely on data from external sources
than it is to include data in the program itself. Using external data allows for
flexibility in programming and tailoring of the program to fit the company's or
the user's needs.
Interactive is the term used with programs that allow the user to interact
with the program by making choices, entering data, and viewing results.
Interactive input and output in Java usually involves a user of the program
entering data using a mouse and keyboard and viewing results on the screen.
Streams and the System Class
In Java, the act of data flowing in or out of a program is called a stream .
Examples of streams include data being input from a keyboard or output to a
display. Recall that the System class, which was introduced in Chapter 2, contains
several useful methods, including those involving standard input and output
streams. The System class creates three different streams when a program exe-
cutes: System.in, System.out, and System.err. Table 3-4 on the next page
describes these three System streams or classes. Each has many associated
methods; several examples of these methods are listed in the table.
Search WWH ::




Custom Search