Espresso: A Concentrated Introduction to Java
Summary: We introduce numeric and mathematics classes in the Java programming language.
Prerequisites: Input and Output in Java, and Strings in Java.
Important Classes
java.lang.Double
java.lang.Float
java.lang.Integer
java.lang.Long
java.lang.Number
java.math.BigDecimal
java.math.BigInteger
java.lang.Math
Contents:
For each primitive numeric type, Java has a corresponding numeric class.
The numeric classes are java.lang.Short,
java.lang.Integer, java.lang.Long,
java.lang.Float, and java.lang.Double.
All are based on the more general class java.lang.Number.
You can think of each of these classes as "wrapping" a primitive data value
inside
an object, and for this reason we also call these classes "wrapper" classes.
Why would we want to wrap a primitive numeric type inside an object?
Because the numeric classes provide additional functionality (via method
calls) that is not available to primitive numeric types.
Perhaps the most important thing the numeric classes allow us to do is to
take a string that represents a number and parse
it to determine what the
number is. Keep in mind that while we have all learned to read a string
such as 3.14159
and quickly detect what number it represents, the
task is more difficult for a computer. To parse the string
algorithmically, we must consider each character in turn and interpret its
contribution to the result based on its position within the
string. Moreover, we must detect errors within the string that cause it to
be invalid (e.g., 3.1415.9
). It is
nice to have built-in methods that will do all this for us!
For example, suppose we want a user to enter a number, which we expect to
be an integer. We can convert the input from a String to an
int as follows.
pen.print("Enter grade: ");
pen.flush();
int grade = Integer.parseInt(eyes.readLine());
In addition, each numeric class provides an appropriate constructor (e.g.,
the constructor
for Integer accepts an int as a parameter)
that can be invoked as follows:
Double d; d = new Double(3.2);or more simply:
Integer i = new Integer(5);
The general Number class (and therefore all the related classes) also provides
a number of conversion methods, including shortValue,
intValue, longValue, floatValue,
and doubleValue. Each of these methods returns a value of
the specified type. In the following example, we convert the literal value
3.14 (which by default is interpreted as a
double) into a float.
Double d = new Double(3.14); float f = d.floatValue();
Because some progams need values that go beyond the ranges of the primitive
types (ranges that are duplicated by the corresponding number classes), Java
also provides two kinds of arbitrary size
numbers:
java.math.BigDecimal and
java.math.BigInteger.
These classes provide their own methods for the basic arithmetic and comparison
operations. The basic operations are add,
subtract, multiply, and divide.
Because these are classes, the operations are written in the
object-oriented object dot method format.
For example, to multiply x by y and then
subtract z, you might write:
BigInteger result, x, y, z; ... result = x.multiply(y) result = result.subtract(z);
Since the expression x.multiply(y) returns a
BigInteger value, we can also write the previous code more
compactly. In this example, we are calling the method subtract
on the BigInteger returned by x.multiply(y).
BigInteger result, x, y, z; ... result = x.multiply(y).subtract(z);
However, we cannot simply write the expression using the familiar arithmetic operators. (The example below does not compile.)
BigInteger result, x, y, z; ... result = x * y - z;
The methods associated with BigNumbers are necessarily less efficient than
the corresponding operations for
the primitive classes. Nonetheless, if you need to represent numbers
beyond the normal ranges (or with greater precision, in the case of
BigDecimal), they are excellent options.
What do you do if you want to perform more complex mathematical
calculations, such as finding square roots? You use the useful
class java.lang.Math. You should refer to the
documentation for that class for more information.
Note that the calls to most of the methods in java.lang.Math
take a slightly different form than you are used to. Rather than writing
objectName.methodName(params), you
write Math.methodName(params), as in
double root; root = Math.sqrt(x);