Java Reference
In-Depth Information
types. The short type creates a short integer. Variables of type short are appropriate when
you don't need the larger range offered by int .
Ask the Expert
Q :
You say that there are four integer types: int, short, long, and byte. However, I
have heard that char can also be categorized as an integer type in Java. Can you
explain?
A : The formal specification for Java defines a type category called integral types, which
includes byte , short , int , long , and char . They are called integral types because they
all hold whole-number, binary values. However, the purpose of the first four is to
represent numeric integer quantities. The purpose of char is to represent characters.
Therefore, the principal uses of char and the principal uses of the other integral types
are fundamentally different. Because of the differences, the char type is treated sep-
arately in this topic.
Floating-Point Types
As explained in Chapter 1 , the floating-point types can represent numbers that have frac-
tional components. There are two kinds of floating-point types, float and double , which
represent single- and double-precision numbers, respectively. Type float is 32 bits wide and
type double is 64 bits wide.
Of the two, double is the most commonly used because all of the math functions in
Java's class library use double values. For example, the sqrt( ) method (which is defined
by the standard Math class) returns a double value that is the square root of its double ar-
gument. Here, sqrt( ) is used to compute the length of the hypotenuse, given the lengths of
the two opposing sides:
Search WWH ::




Custom Search