Chapter 4. Reference Types
Reference types hold references to objects and provide a means to access those objects stored somewhere in memory. The memory locations are irrelevant to programmers. All reference types are a subclass of type java.lang.Object.
Table 4-1 lists the five Java reference types.
| Reference type | Brief description |
Annotation | Provides a way to associate metadata (data about data) with program elements. |
Array | Provides a fixed-size data structure that stores data elements of the same type. |
Class | Designed to provide inheritance, polymorphism, and encapsulation. Usually models something in the real world and consists of a set of values that holds data and a set of methods that operates on the data. |
Enumeration | A reference for a set of objects that represents a related set of choices. |
Interface | Provides a public API and is “implemented” by Java classes. |
Comparing Reference Types to Primitive Types
There are two type categories in Java: reference types and primitive types. Table 4-2 shows some of the key comparisons between them. See Chapter 3 for more details.
| Reference types | Primitive types |
Unlimited number of reference types, as they are defined by the user. | Consists of |
Memory location stores a reference to the data. | Memory location stores actual data held by the primitive type. |
When a reference type is assigned to another reference ... |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access