3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Define reference types in Java.
- Create objects using reference variables.
- Differentiate between primitive and reference types.
- Assign and use object references.
- Apply reference types in Java programs.
3.2 Overview
Reference types in Java store references (memory addresses) to objects instead of actual values. Classes, arrays, strings, and objects are examples of reference types.
This lesson introduces learners to the practical use of reference types and object references in Java applications.
Reference types are important in:
- object-oriented programming,
- memory management,
- software applications,
- and data structures.
Understanding reference types is important because modern Java applications rely heavily on objects and object references.
PA0701 — Define Reference Types
Reference types store references to objects in memory.
Examples of Reference Types
|
Reference Type |
Description |
|---|---|
|
String |
Stores text |
|
Arrays |
Stores collections |
|
Classes |
Stores objects |
|
Objects |
Instances of classes |
Primitive vs Reference Types
|
Primitive Type |
Reference Type |
|---|---|
|
Stores actual value |
Stores memory reference |
|
int |
String |
|
double |
Arrays |
|
boolean |
Objects |
Practical Activity
Learners must:
- identify reference types,
- compare primitive and reference types,
- and explain object references.
PA0702 — Create Objects Using Reference Variables
Objects are created using the:
Plain text
new
keyword.
Example Object Creation
Java
String name = new String(“Alice”);
Java
Student student = new Student();
Practical Activity
Learners must:
- create objects,
- assign reference variables,
- and display object information.
PA0703 — Assign Reference Variables
Reference variables can point to objects in memory
Example
Java
Student s1 = new Student();
Student s2 = s1;
Explanation
Both variables reference the same object in memory.
Practical Activity
Learners must:
- assign reference variables,
- compare references,
- and observe shared object behavior.
PA0704 — Use null References
Reference variables can contain:
Plain text
null
when they do not reference an object.
Example
Java
Student student = null;
NullPointerException Example
Java
student.display();
Practical Activity
Learners must:
- assign null references,
- identify null reference errors,
- and prevent runtime exceptions.
PA0705 — Compare Reference Variables
Reference variables can be compared using:
Plain text
==
operator.
Example
Java
String a = “Java”;
String b = “Java”;
System.out.println(a == b);
Practical Activity
Learners must:
- compare object references,
- test equality,
- and display comparison results.
PA0706 — Create a Reference Type Program
Reference types are commonly used in object-oriented applications.
Example Program
Java
class Student {
String name;
}
public class ReferenceExample {
public static void main(String[] args) {
Student student = new Student();
student.name = “Alice”;
System.out.println(student.name);
}
}
Importance of Reference Types
Reference types support:
- object-oriented programming,
- dynamic memory usage,
- and reusable software structures.
3.5 Key Notes / Summary
- Reference types store object references in memory.
- Objects are created using the
newkeyword. - Strings, arrays, and classes are reference types.
- null means no object reference exists.
- Reference variables can point to the same object.
- Reference types are important in object-oriented programming.