University of Chicago, CS 102, Spring 2006, Notes 4

previous  |  toc  |  next


Java has two kinds of types: primitive types and reference types.

Let's take the elements-and-atoms analogy a step further. Recall that primitive types are like elements, and primitive values are like atoms. Along those lines, reference types are like compounds, and reference values, i.e. objects, are like molecules.

That is, the compound H2O represents some category of molecules. Think of H2O as a reference type. Think of particular H2O molecules as objects (i.e., reference values) of that type.

When talking about Java, objects are the same as reference values or values or reference types. Every class definition is the definition of a reference type. We define the reference type Fraction by writing its class definition; the values representing fractions are objects of type Fraction.

The names of all primitive types in Java begin with lowercase letters. There are only eight primitive types, and this characteristic is true of all of them, so it is always true. The names of reference types, on the other hand, start (by convention) with uppercase letters. This is not strictly enforced by the language, but it is a rule that essentially every Java programmer follows. As a result, for our purposes you may take it as fact that all reference types begin with capital letters.

There is one reference type you should know about immediately: the String reference type. String values are written as chunks of text surrounded by quotation marks. Examples:

"Sally"
"Sally Field"
"I am terribly fond of Sally Field."
You will find that you will often want to use Strings for private instance variables; they arise naturally all the time. Consider what might be included in the definition of a Person class.
public class Person {
  // private instance variables
  private String lastName;
  private String firstName;
  private int height; // in inches
  private int weight; // in pounds
}

Two sets of notes ago, we also defined two reference types of our own: Point and Fraction.

The building blocks of objects are both primitive values and objects.

The class definitions from two sets of notes ago -- Point, Fraction, and the optional exercises -- all had the following characteristic: all of their private instance variables were of primitive types.

This must not always be the case. Consider the Person class above. Of its four private instance variables, two are or primitive types (the two ints), and two are of reference types (the two Strings).

In general, the instance variables in a class definition may be of either primitive types or reference types.

Consider line segments. If we were to build a LineSeg class, it would be most natural to build it out of a pair of Points, as follows:

public class LineSeg {
  // private instance variables
  private Point a;
  private Point b;
}

We have thus far seen only class definitions in Java. We haven't yet seen any objects (apart from some String objects we've seen in passing). The next section discusses how you can actually write down objects in Java programs.

previous  |  toc  |  next