AmouAI Hub/Courses/Programming Fundamentals/Day 16
Java: The Model Shift
Every word of public static void main, explained. Compilation, the JVM, and static types as a new set of rules for ideas you already own.
By the end of today you can
- Explain what the compiler does that Python's interpreter does not
- Say what the JVM is and why "write once, run anywhere" was the pitch
- Declare a variable with an explicit type, and read a type error
- Tell a primitive from a reference, and predict which one
==compares - Write, compile and run a Java program from the command line
- Explain every word of
public static void main(String[] args) - Translate a small Python program into Java without guessing
▶Today's videos
Watch each video, then work the matching sections below. Watching alone will not do it.
javac checks that Python never does · source, bytecode and the JVM · declaring types · reading a compile error · primitives versus references · null and why it is not None.public static void main(String[] args) · System.out.println · braces where indentation used to be · translating a small Python program line by line · what transferred and what only changed spelling.1Compiled versus interpreted
The same idea, checked at a different moment.
Fifteen days of Python have given you a mental model in which a program is text that runs. Java changes when the checking happens, and almost everything else follows from that one change.
| Python | Java | |
|---|---|---|
| Type errors found | When the line runs | Before anything runs |
| Typo in a name | NameError, at runtime | Compile error, immediately |
| Run a broken program | Yes — until it reaches the bug | No — it will not compile |
| Feedback speed | Instant | One compile step slower |
| Refactoring safety | You find out by testing | The compiler finds most of it |
Java makes you say more up front and rejects more of what you write. In exchange, a whole class of bug — the one where line 400 fails because of a typo, forty minutes into a run — becomes impossible. Neither choice is correct in general. Which one fits depends on the size of the program and the size of the team, and you are about to have an informed opinion for the first time.
2Every word of the magic incantation
public static void main(String[] args)
javac Hello.java # compile — produces Hello.class
java Hello # run — note: no .class, no .java
# since Java 11, for a single file, one step:
java Hello.java3Types, primitives and references
The distinction Python hides and Java puts in your face.
int count = 5; // the type comes first
double price = 19.99;
boolean ready = true; // lower case, unlike Python's True
char grade = 'A'; // single quotes = one character
String name = "Amin"; // double quotes = a String object
count = "five"; // COMPILE ERROR, before anything runsIn Python that mistake would happen at runtime, possibly weeks later, possibly in front of a user. Here it happens before the program exists.
The eight primitives, and everything else
| Primitives | References | |
|---|---|---|
| Examples | int, double, boolean, char, long, float, byte, short | String, arrays, every class you write |
| Hold | The value itself | A pointer to an object |
| Start as | 0, 0.0, false | null |
== compares | The values — what you want | The addresses — usually not what you want |
| Spelling | lower case | Capitalised |
For primitives == does what you expect. For objects it asks are these the same object?, not do these have the same contents? Two Strings with identical text can be == false. Tomorrow is largely about this.
null has no Python equivalent
null looks like Python's None, and it is not the same. None is an object you can pass around safely. null is the absence of an object — call a method on it and you get a NullPointerException, historically the most common failure in Java programs.
Look at the bodies of those two functions. Identical. Every idea from Weeks 1-3 transfers: variables, conditionals, loops, functions, objects. You are learning a new set of rules, not a new way of thinking. That is exactly why Java comes second.
>_Python playground
A real Python interpreter running inside your browser. Nothing is installed, nothing is uploaded, nothing can break.
input(), comma separated →
✓Exercise set
Checked automatically the moment you submit. Work top to bottom — each one assumes the last. Your answers are saved in this browser.
Day 17 — Java Control Flow, Arrays & Methods
Familiar structures, unfamiliar rules — including the == trap.