Java Reference
In-Depth Information
the order of operations by applying the cast to the whole expression, using paren-
theses.
SR5 . Invent some declarations and assignments yourself so that you become
thoroughly familiar with how they work. Include attempts to declare a variable
several times, assignments where you have to cast, and expressions that contain
calls to functions. (It is fun to see what happens when you intentionally do things
wrong. Do not worry; you will not break anything.) Use all the types we have
introduced: int , double , boolean , and String .
1.3
Classes and objects
1.3.1
The class as a file drawer of folders
Thus far, you have seen mostly primitive types: int , boolean , double , and so
on. But Java is object-oriented . In this section, we show you what that means:
what objects are and how they are created and used. Objects contain both data
(using variables) and instructions (using methods), much like manila folders con-
tain data and instructions in an office filing system. In fact, the similarities are
strong, and we use the analogy throughout this text.
An office typically has filing cabinets (see Fig. 1.3). Each drawer of a filing
cabinet contains manila folders. For example, each folder in a Patient file draw-
er might contain data for a different patient (patient name, billing information,
and so on). Often, all the manila folders in a filing cabinet drawer have the same
kind of data. For example, in a doctor's office, Patient folders are not usually
in the same drawer as folders containing information related to building mainte-
nance.
The tab of each manila folder has a unique name. To generate unique names
easily, you can use the sequence a0 , a1 , , a9 , b0 , b1 , … b9 , c0 , c1 , …. We will
use these names to uniquely identify which particular object a variable refers to.
(In the computer, the name is a memory address chosen by the operating system.
When tracing by hand, it can be any unique name.)
a0
a1
Patient
Patient
name
P. Gries
name
D. Gries
getName()
billPatient( double )
getName()
billPatient( double )
Figure 1.3:
Two folders of class Patient
Search WWH ::




Custom Search