Programming and Problem Solving with Java

(やまだぃちぅ) #1
10.2 C o m posite Data Types | 481

Arithmetic operations may be applied to character data (and often are). The only difference is
that when character data is printed, the character itself is printed rather than the numeric rep-
resentation of the character. In Java, applying arithmetic operations to character data causes
the compiler to insert a cast operation that converts the character to a numerical value.
The Boolean data type includes two literals, true and false. The operations allowed on
these values are the logical operators AND, OR, and NOT ; the relational operations; and as-
signment. Although many languages consider Boolean values to be ordered, Java does not,
so only equal and not equal can be applied to Boolean values in Java. Java calls the Boolean
type booleanand the constants trueand false.
Integers, reals, characters, and Booleans are called atomicor simple elementsbecause they
have no component parts that can be accessed separately. They are also ordered. Types that
are atomic and ordered are called scalar data types.
For example, a single character is atomic, but the string"Good Morning"is not
because it is composed of 12 characters. When we say that the values are ordered,
we mean that exactly one of the relations less than, greater than, or equal holds true
for any pair of values. For example,


1 < 2 'C'> 'A' 3.562< 106.22 false== false


Integers, characters, and Booleans have yet another property: Each value (ex-
cept the first) has a unique predecessor and each value (except the last) has a
unique successor. Types with this property are called ordinal data types. Although
theoretically Booleans are ordinal, Java’s type booleanis not.
Real numbers are not ordinal because a real value has no unique predeces-
sor or successor. If one more digit of precision is added, the predecessor and suc-
cessor change; that is, 0.52 and 0.520 are the same, but the predecessor of 0.52 is
0.51, and the predecessor of 0.520 is 0.519. Because Java’s type booleanis not or-
dered, it is not ordinal.
Although the ordinals form a subset of the scalars, the two types are quite dif-
ferent. Mathematicians make this same distinction when they talk about con-
tinuous values versus discrete values. Many real-life analogies demonstrate this distinction
as well: the spectrum of colors in a real rainbow versus the discrete colors in a child’s crayon
drawing of a rainbow or the continuous tone of a violin sliding up the scale versus the dis-
crete tones of a piano.
Java classifies the atomic types that it represents as “primitive.”


10.2 Composite Data Types


Sometimes we may need to show a relationship among variables or to store and reference
collections of variables as a group. For this reason, we need a way to associate an identifier
with a collection of values. A data type made up of a collection of values is called a compos-
ite data type.


Atomic (simple) elements
Elements that have no compo-
nent parts
Scalar data type A data type
in which the values are ordered
and each value is atomic (indi-
visible)
Ordinal data type A data type
in which each value (except the
first) has a unique predecessor
and each value (except the last)
has a unique successor
Composite data type A data
type that allows a collection of
values to be associated with an
identifier of that type
Free download pdf