Chapter 13 · Week 13

Shifting Gears — Hello, Java

"For we walk by faith, not by sight." — 2 Corinthians 5:7
13

See the same program in both languages

Project 9's Stewardship Account, side by side in C++ and Java. Click any line to see what changed. Most of the OO logic ports directly — the ceremony around it changes.

C++ → Java — The Same Program

Project 9's Stewardship Account, ported. Click any line on either side to highlight its counterpart and read the note. Most of the OO logic is identical — what changes is the ceremony around memory and types.

C++account.cpp
 
JavaAccount.java + Demo.java
 
Click any line to see what changed between the two languages.
The biggest invisibles: Java's references replace C++'s pointers (no *, no ->); Java's garbage collector replaces delete and the destructor; Java requires one public class per file. The OO design is the same.

Why This Matters

For twelve chapters you drove a stick shift. You picked the gear, operated the clutch, managed memory by hand. Today you sit down in an automatic.

Java took the OO power of C++ and hid the hard parts. Garbage collection replaces new/delete. References replace pointers. The OO design survives. Some syntax simplifies; some gets fussier (one public class per file). You did not relearn programming this week. You learned new vocabulary for things you already know.

The First Java Program

// File: Hello.java
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, world.");
    }
}
  • One public class per file; filename must match the class name.
  • main lives inside a class, marked public static void.
  • System.out.println is Java's cout << ... << endl.

The Five Types — Renamed

C++JavaNotes
intintsame
doubledoublesame
boolbooleanfull word in Java
charcharJava is 16-bit Unicode
stringStringcapital S — it's a class

The == vs .equals() Gotcha

String a = "hello";
String b = new String("hello");
System.out.println(a == b);         // false (different references)
System.out.println(a.equals(b));    // true  (same content)

For primitives use ==; for objects use .equals(). This is the canonical Java gotcha. Project 13 will catch you on it at least once.

No Pointers — But References Everywhere

In Java, every variable of a class type is implicitly a reference. Account a = new Account("Maya", 100); creates an Account on the heap and stores a reference to it in a. No *, no ->, no delete. a.deposit(50) reads as method call but is actually a follow-the-reference operation.

The garbage collector frees objects when no reference points to them anywhere. Memory leaks in the C++ sense are essentially impossible. Pointer bugs evaporate.

Coach's Note — If you internalized the C++ chapters honestly, the GC won't feel like cheating — it'll feel like a correct default. You spent Chapter 11 doing pointer arithmetic, freeing nodes, running AddressSanitizer. Java does all of that automatically. Use the time you saved on better problems.

This Week's Project

You're ready for Project 13: Java Migration. Port one of your earlier C++ projects to Java. Three tiers:

  • Normal: Port Project 9 (Stewardship Account).
  • Medium: Port Project 10 (Apologetics Library), using ArrayList.
  • Hard: Port Project 11 (Chain of Witnesses) — build the linked list by hand, no LinkedList<> shortcut.

You'll also write migration-notes.txt documenting at least 5 specific things you noticed about what changed and what didn't.

Check Your Reps

Hello, Java — Quick Check

Question 1 of 4
What does this print?
String a = "hello";
String b = new String("hello");
System.out.println(a == b);
Why: == on String compares object identity, not content. a is an interned literal; b is a separate object created with new. Different references → false. For content comparison: a.equals(b) returns true.
Question 2 of 4
Which file naming is required for a Java class declared public class Account?
Why: Java enforces filename = class name (case-sensitive) for any public class. account.java fails to compile. C++ has no such requirement.
Question 3 of 4
You write Account a; in Java (no new). What is a?
Why: In Java, class-typed variables are references. Without new, a is initialized to null. Calling a.deposit(50) throws NullPointerException. This is different from C++, where Account a; default-constructs an actual object on the stack.
Question 4 of 4
In Java, when does the equivalent of a destructor run?
Why: Java has finalize (deprecated in modern versions) which fires when the GC reclaims an object — but at an unpredictable time. You don't write code that depends on it. Resource cleanup is done with try-with-resources or explicit close(). Practically: Java has no deterministic destructor, and most C++ destructor code simply disappears in the port.
YOU FINISHED. NICE WORK.

← WEEK 12: INHERITANCE   ·   WEEK 14: POLYMORPHISM →