Shifting Gears — Hello, Java
"For we walk by faith, not by sight." — 2 Corinthians 5:7
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.
mainlives inside a class, markedpublic static void.System.out.printlnis Java'scout << ... << endl.
The Five Types — Renamed
| C++ | Java | Notes |
|---|---|---|
int | int | same |
double | double | same |
bool | boolean | full word in Java |
char | char | Java is 16-bit Unicode |
string | String | capital 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.
Hello, Java — Quick Check
String a = "hello";
String b = new String("hello");
System.out.println(a == b); == 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. public class Account?public class. account.java fails to compile. C++ has no such requirement. Account a; in Java (no new). What is a?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. 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.