Keywords: current

Next: 1.3 Java Class Definitions Up: 1.2 Java Data Types Previous: 1.2.1.0.2 Conversions Between Types

## 1.2.2 Object Types

Object values are created by instantiating classes, which may either be built-in or program-defined. Classes are organized in a strict hierarchy with the special built-in class `Object` at the top. Every class C except `Object` has a unique parent in the hierarchy called the superclass of C. A descendant in the class hierarchy is called a subclass. Each subclass of a class C includes all of the members (fields and methods) of C and possibly additional members. For this reason, we say that each immediate subclass of (child of) C extends C. Note that subclass relation is transitive and reflexive. If class A is a subclass of class B and class B is a subclass of class C then A is a subclass of C (transitivity). In addition, every class A is a subclass of itself (reflexivity).

We will reserve the term extends to describe the immediate subclass (child) relation: A extends B iff A is an immediate subclass of B. Hence, the extends relation is neither transitive nor reflexive. Since `Object` is the top class of the hierarchy, all classes are subclasses of `Object`.

For example, the built-in classes `Integer` and `Float` extend the built-in class `Number` which extends `Object`. Hence, the superclass of `Integer` is `Number`, the superclass of `Float` is `Number`, and the superclass of `Number` is `Object`.

Object values are actually references to objects. For this reason, two different fields can be bound to exactly the same object. In Java, objects are never implicitly copied. When a field or method parameter v is bound to an object o, the value of v is a reference to the object o, not a copy of o!

Two objects can be compared for identity by using the `==` comparison operator.

Finger Exercise: In the DrJava Interactions pane, try evaluating the following sequence of statements and expressions:

    Integer i = new Integer(5);
    Integer j = i;
    Integer k = new Integer(5);
    i == j
    i == k

Did you get the answers that you expected?

Every Java class C has an associated type C consisting of all instances of class C and all of its subclasses. Hence, the type `Object` contains all object values. The built-in class `String` has the class `Object` as its superclass. Since the class `String` has no subclasses, the only values of type `String` are instances of the class `String`. In contrast, the built-in class `Number` is a child of class `Object` and has several subclasses including `Integer` and `Float`. Hence, all instances of the classes `Integer` and `Float` are values of type `Number`.

In Java, every field and method has a declared type given as part of its definition. For a method, the declared type includes the type of the result and the types of the parameters.

Java determines the type of every program expression using a simple set of rules and confirms that

- the value assigned to a field is consistent with the field's declared type;
- the values passed as arguments to a method are consistent with the corresponding parameter types;
- the value returned by a method is consistent with the declared return type of the method; and
- the member name in a field reference or method invocation is compatible with the static type of the receiver.

We will discuss these ``type-checking'' rules in more detail in Section 1.9.2.

Next: 1.3 Java Class Definitions Up: 1.2 Java Data Types Previous: 1.2.1.0.2 Conversions Between Types