Java Reference
In-Depth Information
head. When we think about what our program will do, we will think about the object structures
it creates and how these objects interact. Being able to visualize the object structures is essential.
Exercise 3.1
Think again about the
lab-classes
project that we discussed in Chapter 1 and
Chapter 2. Imagine that we create a
LabClass
object and three
Student
objects. We then
enroll all three students in that lab. Try to draw a class diagram and an object diagram for that
situation. Identify and explain the differences between them.
Exercise 3.2
At what time(s) can a class diagram change? How is it changed?
Exercise 3.3
At what time(s) can an object diagram change? How is it changed?
Exercise 3.4
Write a definition of a field named
tutor
that can hold a reference to an
object of type
Instructor
.
3.7
Primitive types and object types
Java knows two very different kinds of type:
primitive types
and
object types.
Primitive
types are all predefined in the Java language. They include
int
and
boolean
. A complete
list of primitive types is given in Appendix B. Object types are those defined by classes.
Some classes are defined by the standard Java system (such as
String
); others are those
classes we write ourselves.
Concept:
The
primitive
types
in Java are
the non-object
types. Types such
as
int
,
boolean
,
char
,
double
,
and
long
are the
most common
primitive types.
Primitive types have
no methods.
Both primitive types and object types can be used as types, but there are situations in which
they behave differently. One difference is how values are stored. As we could see from our dia-
grams, primitive values are stored directly in a variable (we have written the value directly into
the variable box—for example, in Chapter 2, Figure 2.3). Objects, on the other hand, are not
stored directly in the variable, but instead a reference to the object is stored (drawn as an arrow
in our diagrams, as in Figure 3.3a).
We will see other differences between primitive types and object types later.
3.8
The
ClockDisplay
source code
Before we start to analyze the source code, it will help if you have a look at the example.
Exercise 3.5
Start BlueJ, open the
clock-display
example, and experiment with it. To use
it, create a
ClockDisplay
object using the constructor that takes no parameters, then open
an inspector window for this object. With the inspector open, call the object's methods. Watch
the
displayString
field in the inspector. Read the project comment (by double-clicking the
text note icon on the main screen) to get more information.
Search WWH ::
Custom Search