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'snulltriggers aNullPointerException. - The Fix: You must create the object in memory using the
newoperator 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
maincallsadd(), 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;:
- Heap: The actual
SUNDAYobject is created at a specific address (e.g., Address 100). - Method Area: The name
Week.SUNDAYholds the address "100". - Stack: Your variable
todaycopies that address "100". - Both point to the same object, which is why
==comparison returnstrue.
8. The Calendar API Quirks
- No
newkeyword:Calendaris an abstract class. We usegetInstance()to ask Java for an object suited to our current environment (e.g., Korea/Canada). - The
MONTH + 1Rule: 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
whileloop if you need to persist data across iterations. - Packages: If your Enum is in a different folder, don't forget the
importstatement! 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)