Java Reference
In-Depth Information
2.3 Primitive Data Types
There are eight primitive data types in Java: four subsets of integers, two sub-
sets of floating point numbers, a character data type, and a boolean data type.
Everything else is represented using objects. Let's examine these eight primitive
data types in some detail.
Integers and Floating Points
Java has two basic kinds of numeric values: integers, which have no fractional
part, and floating points, which do. There are four integer data types ( byte ,
short , int , and long ) and two floating point data types ( float and double ). All
of the numeric types differ by the amount of memory space used
to store a value of that type, which determines the range of values
that can be represented. The size of each data type is the same for
all hardware platforms. All numeric types are signed, meaning that
both positive and negative values can be stored in them. Figure 2.2
summarizes the numeric primitive types.
Recall from our discussion in Chapter 1 that a bit can be either a 1 or a 0.
Because each bit can represent two different states, a string of N bits can be used
to represent 2 N different values. Appendix B describes number systems and these
kinds of relationships in more detail.
When designing programs, we sometimes need to be careful about picking
variables of appropriate size so that memory space is not wasted. This occurs in
situations where memory space is particularly restricted, such as a program that
runs on a personal data assistant (PDA). In such cases, we can choose a variable's
data type accordingly. For example, if the value of a particular variable will not
KEY CONCEPT
Java has two kinds of numeric
values: integer and floating point.
There are four integer data types and
two floating point data types.
Type
Storage
Min Value
Max Value
byte
8 bits
-128
127
short
int
16 bits
32 bits
-32,768
-2,147,483,648
32,767
2,147,483,647
long
float
64 bits
32 bits
-9,223,372,036,854,775,808
Approximately -3.4E+38
with 7 significant digits
Approximately -1.7E+308
with 15 significant digits
9,223,372,036,854,775,807
Approximately 3.4E+38
with 7 significant digits
Approximately 1.7E+308
with 15 significant digits
double
64 bits
FIGURE 2.2
The Java numeric primitive types
 
Search WWH ::




Custom Search