Java Reference Data Types – Core Notes

Concise points about non-primitive (reference) types and related concepts.


Table of Contents


1. What Is a Reference Type?


2. Object References and Mutability

Employee empObj = new Employee();
empObj.empId = 110010;

3. Passing Objects to Methods

modify(empObj);

void modify(Employee e) {
    e.empId = 110025;
}

4. Strings as Reference Types

String s1 = "hello";
String s2 = "hello";

4.1 Literal vs new String()

String s1 = "hello";            // pooled
String s3 = new String("hello"); // new heap object

5. Interfaces as Reference Types

Person p = new Engineer();

6. Primitive vs Reference (Snapshot)

Concept Primitive Reference
Stores Value Address
Method passing Copy of value Copy of reference
Can be null
Mutation visible

7. Wrapper Classes

Primitive Wrapper
int Integer
double Double
char Character
boolean Boolean

Why: collections/APIs expect objects; wrappers add methods; allow null.


8. Autoboxing & Unboxing

int a = 10;
Integer obj = a;      // autoboxing -> Integer.valueOf(a)
int b = obj;          // unboxing   -> obj.intValue()

9. Wrapper Caching (Favorite)

Integer a = 100, b = 100; // cached → a == b is true
Integer x = 200, y = 200; // new objects → x == y is false

10. Constants (final / static final)

final int MAX = 100;
public static final int MAX_USERS = 1000;
public static final Integer LIMIT = 10; // wrapper constant (avoid null)

Prefer primitives for constants unless an object is required.


11. Collections Require Wrappers

// List<int> list = new ArrayList<>(); // ❌
List<Integer> list = new ArrayList<>(); // ✅

list.add(10);        // autoboxing
int x = list.get(0); // unboxing

12. Performance Note


13. Primitive vs Wrapper (Quick Compare)

Feature Primitive Wrapper
Object
Can be null
Collections
Performance Faster Slower
Methods

14. Carry-Forward Points


← Back to Basics ☕ Java Hub