Skip to content
ajdevhub
4 min read

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?

  • Stores an address to an object (not the data).
  • Examples: class objects, strings, arrays, interfaces.
  • Mental model: variable = remote control; object = TV.

2. Object References and Mutability

Employee empObj = new Employee();
empObj.empId = 110010;
  • empObj holds a reference; fields live inside the object.
  • Multiple references can point to the same object.

3. Passing Objects to Methods

modify(empObj);

void modify(Employee e) {
    e.empId = 110025;
}
  • Java is pass-by-value; the reference value is copied.
  • Mutations via the copy are visible to all references.
  • You cannot rebind the caller’s reference from inside the method.

4. Strings as Reference Types

String s1 = "hello";
String s2 = "hello";
  • Strings are reference types but immutable.
  • Pooling allows s1 and s2 to share the same object.
  • == compares references; equals() compares content (always use equals()).

4.1 Literal vs new String()

String s1 = "hello";            // pooled
String s3 = new String("hello"); // new heap object
  • Same content, different references; new String() always creates a new object.

5. Interfaces as Reference Types

Person p = new Engineer();
  • Interface reference can point to any implementing class; dispatch is runtime.
  • Cannot instantiate an interface directly (new Person() ❌).
  • Principle: program to an interface, not an implementation.

6. Primitive vs Reference (Snapshot)

ConceptPrimitiveReference
StoresValueAddress
Method passingCopy of valueCopy of reference
Can be null❌✅
Mutation visible❌✅

7. Wrapper Classes

  • Convert primitives to objects.
PrimitiveWrapper
intInteger
doubleDouble
charCharacter
booleanBoolean

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()
  • Compiler inserts conversions.
  • Unboxing a null wrapper throws NullPointerException.

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
  • Cache range: -128 to 127.
  • Use equals() for comparison.

10. Constants (final / static final)

final int MAX = 100;
public static final int MAX_USERS = 1000;
  • final: cannot be reassigned; encourages immutability.
  • static final: one shared, compile-time constant; name in UPPER_CASE_WITH_UNDERSCORES.
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
  • Collections store objects; wrappers bridge the gap.

12. Performance Note

  • Autoboxing allocates; avoid in tight numeric loops.
  • Rule: use primitives for computation; wrappers for collections/APIs.

13. Primitive vs Wrapper (Quick Compare)

FeaturePrimitiveWrapper
Object❌✅
Can be null❌✅
Collections❌✅
PerformanceFasterSlower
Methods❌✅

14. Carry-Forward Points

  • References point to objects; mutations are shared across references.
  • Java passes references by value.
  • Strings are immutable; compare with equals().
  • Wrappers enable collections/APIs and null; mind autoboxing cost.
  • Use equals() for wrappers; use static final for true constants.

← Back to Basics ☕ Java Hub