Programming and Problem Solving with Java

(やまだぃちぅ) #1
1.5 What Kinds of Instructions Can Be Written in a Programming Language? | 19

only simple data typessuch as integers and real numbers, which have obvious
sets of operations defined by mathematics. Those operations were built directly
into early programming languages. Each kind of data in the computer was said
to have a specific data type. For example, if we say that two data items are of type
int(a name that Java uses for integer numbers), we know how they are represented
in memory and that we can apply arithmetic operations to them.
As people gained experience with the programming process, they realized that
in solving complex problems, it is helpful to define new types of data, such as dates
and times, which aren’t a standard part of a programming language. Each new type
of data typically has an associated set of operations, such as determining the
number of days between two dates.
Procedural languages thus evolved to include the feature of extensibility: the
capability to define new data types. However, they continued to treat the data
and operations as separate parts of the program. A programmer could define a data
type to represent the data values making up the time of day and then write a
subprogram to compute the number of minutes between two times, but could not explicitly
indicate that the two were related.
Modern programming languages such as Java allow us to collect data and its associated
operations into objects. For this reason, they are called object-orientedprogramming lan-
guages. The advantage of an object is that it makes the relationships between the data and
operations explicit. Each object is a complete, self-contained unit that can be reused again
in other applications. This reusability enables us to write a significant portion of our code us-
ing existing objects, thereby saving a considerable amount of time and effort.
Most modern programming languages, Java included, retain vestiges of their procedural
ancestors in the form of a small set of primitive data types. These usually include integer and
real numbers, characters, and a type representing the values true and false. Java also defines
the object as one of its primitive data types. In Java, all objects are said to be of the same data
type—the type that lets us represent any object. We distinguish among different kinds of ob-
jects by referring to the classes that define them. For example, we may refer to an object of
the class String. Sometimes we may refer to objects as being of different types, which is
common terminology in the computer industry. In such a case, we really mean objects of dif-
ferent classes, as there is strictly just one type of object in Java.
As noted earlier, a class is a description of an object. When we need an object in an ap-
plication, we must create one from such a description. Java provides an operation to let us
do so, and we say that the operation instantiatesthe class. That is, we write an instruction in
Java that provides us with an instance of the object described by the specified class.
One characteristic of an object-oriented programming language is the presence of a
large library of classes. Within the library, classes are usually collected into groups called
packages.
In this book we present only a small subset of the many classes that are available in the
Java library. It is easy to become overwhelmed by the sheer size of Java’s library, but many of
those thousands of objects are highly specialized and unnecessary for learning the essen-


Data type The specification in
a programming language of
how information is represented
in the computer as data and the
set of operations that can be
applied to it
Instantiate To create an ob-
ject based on the description
supplied by a class.
Package A collection of
related classes
Free download pdf