141
Summary
Java provides several built-in numeric data types, of which the most commonly used
are intand double. The integral types are based on the mathematical integers, but the
computer limits the range of integer values that can be represented. The floating-
point types are based on the mathematical notion of real numbers. As with integers,
the computer limits the range of floating-point numbers that can be represented. In
addition, it limits the number of digits of precision in floating-point values. We can
write literals of type doublein several forms, including scientific (E) notation. Java pro-
vides the standard mathematical operations to go with these data types: addition (+),
subtraction (-), multiplication (*), division (/), and remainder (%). Java also provides an
increment operation (++) and a decrement operation (--).
Mixing values of the integer and floating-point types in an expression results in au-
tomatic type conversion to achieve compatibility between the operands of all of the
operators. If you aren’t careful, these automatic conversions can have unanticipated
results. It is best to explicitly use type cast operations whenever you need to mix
types within expressions.
Much of the computation of an application is performed in arithmetic expressions.
Expressions can contain more than one operator. The order in which the operations
are performed is determined by precedence rules. In arithmetic expressions, the
unary operators (such as negation) are performed first; then type casts; then multipli-
cation, division, and modulus; and finally addition and subtraction. Multiple
arithmetic operations of the same precedence are grouped from left to right. You can
use parentheses to override the precedence rules.
Not only should the output produced by an application be easy to read, but the for-
mat of the code itself should also be clear and readable. A consistent style that uses
indentation, blank lines, and spaces within lines helps you (and other programmers)
understand and work with your code.
Value-returning class methods are another way of implementing the behaviors of
objects. They are written by specifying staticin the heading. The call appends the
method name to the name of its class, separated by a period.
A BufferedReaderobject returns a string. To input numerical data, we must use other
Java-supplied classes corresponding to the numeric types. These classes provide
methods to parse a string and convert it to a value of the corresponding numeric type.
We can save our class code in separate files and import the class into our
applications. Separating the class code makes it easier to manage the files, makes the
classes easier to reuse, and makes it easier to divide a large project among a team of
programmers.
Quick Check
1.When you assign one reference variable to another, how many copies of the ob-
ject are there? (pp. 100–103)