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 == kDid 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
We will discuss these ``type-checking'' rules in more detail in Section 1.9.2.