Java Reference
In-Depth Information
C H A P T E R 3
Data Types
Most programming languages have data types. Java has several data types. The first characteristic of a
data type is whether it's a primitive or an object. Anything that's not a primitive is an object of some sort.
Primitive Data Types
Primitive means that no class exists to define a variable of that type. Java supports a number of primitive
data types. In the code samples in previous chapters, you saw several variables whose data types were
primitive. Anywhere you saw int or boolean , those were primitive data types (usually called
“primitives”). Primitives fall into several broad categories. From the language's point of view, the
categories don't exist, but they do help people keep the primitives straight from one another.
Integer Primitives
An integer primitive is one whose value can be only an integer. It cannot be a real number (that is, one
with a fractional value). The numbers 0, 1, 16, and 37 are all integers. The number 12.34 is not an integer.
The distinguishing feature between the various integer primitives is how many bits make up each one.
Because a type made of more bits can hold a bigger number, the practical effect of the different number
of bits is to increase the maximum value of that type. The possible detrimental effect of increasing the
number of bits is to consume more memory. Often, it doesn't matter, but it's good practice to use data
elements no larger than you need.
Integer primitives are signed (unless you use the unsigned keyword, which we discuss more in a
moment), so their values range from a negative number to a positive number.
Table 3-1 shows the details of the various integer primitives.
Search WWH ::




Custom Search