DEV Community

Hanna
Hanna

Posted on

πŸš€ #09: Demystifying Reference Types

Curriculum: Hon-Gong-Ja (Java for Self-Study)

Chapters: Chapter 05 (Sec 01, 02, 03)

Duration: 2026-03-19 ~ 2026-03-27 (1st Reading)


1. Reference Types & NullPointerException

A reference variable (Array, Class) being null means it does not hold any memory address.

  • The Risk: Accessing or storing data (e.g., arr[0]) while it's null triggers a NullPointerException.
  • The Fix: You must create the object in memory using the new operator first.
  • Analogy: It’s like pressing buttons on a remote control (variable) when you don’t even have a TV (object).

2. Array Initialization: Size vs. Index

When declaring new int[3], the number inside [] represents the length, not the index.

  • new int[3]: "Build a house with 3 rooms!" (Determines size)
  • arr[0]: "Go into room #0." (Accesses index)
  • Note: If the length is 3, valid indices are only 0, 1, and 2.

3. The Magic of Method Returns

I used to wonder: "If code runs top-to-bottom, why does it jump to a method at the bottom and come back up?"

  • The Bookmark Principle: When main calls add(), the computer places a "bookmark" (Return Address) at the current line and jumps to the method.
  • The Return: Once it hits return, it carries the result back to that bookmark and resumes execution.

4. Why String[] args in the Main Method?

Why is the default input always a String array?

  • Flexibility: It is easier to convert a String to a Number than vice versa. It's the safest way to receive any data type.
  • Dynamic Storage: Since the JVM doesn't know if the user will input 0 or 100 values, an array acts as a flexible container.

5. Multi-dimensional Arrays: The Candy Bag Analogy

In Java, a multi-dimensional array is an "Array of Arrays."

  • scores.length: Number of "Big Bags" (Rows) β†’ e.g., 2
  • scores[0].length: Number of "Candies" inside the first bag (Columns) β†’ e.g., 3
  • To get the total count, you must multiply Rows Γ— Columns manually.

6. Why Arrays Can't "Grow" (The Real Estate Problem)

Unlike Excel, Java arrays have a fixed size because they occupy contiguous memory spaces.

  • If the neighboring "slot" in memory is already occupied by other data, the array cannot expand.
  • The Solution: To increase size, you must "move house." Create a larger new array and use System.arraycopy() to migrate the data.

7. Enumeration (Enum) Types: Address Copying

When you write Week today = Week.SUNDAY;:

  1. Heap: The actual SUNDAY object is created at a specific address (e.g., Address 100).
  2. Method Area: The name Week.SUNDAY holds the address "100".
  3. Stack: Your variable today copies that address "100".
  4. Both point to the same object, which is why == comparison returns true.

8. The Calendar API Quirks

  • No new keyword: Calendar is an abstract class. We use getInstance() to ask Java for an object suited to our current environment (e.g., Korea/Canada).
  • The MONTH + 1 Rule: Java designers treated months as array indices (0 = Jan, 1 = Feb...). You must always add 1 to get the human-readable month.

πŸ’» Practical Pro-Tips

  • Safe Input: Use Integer.parseInt(scanner.nextLine()) to avoid common input buffer issues.
  • Variable Scope: Declare variables outside the while loop if you need to persist data across iterations.
  • Packages: If your Enum is in a different folder, don't forget the import statement! Different folders are treated as "different neighborhoods" in Java.

This post documents my journey as a non-major career changer diving into the world of Java backend development. Feedback is always welcome!

Top comments (0)