Day 16 / 25 Java: The Model Shift 0/0 exercises Exercises ↓

AmouAI Hub/Courses/Programming Fundamentals/Day 16

Week 4 · Java: A Second Lens · 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.

Study time
4 hours
Reading
Head First Java, Ch. 1–2
Focus
compilation · types · the JVM

By the end of today you can

  1. Explain what the compiler does that Python's interpreter does not
  2. Say what the JVM is and why "write once, run anywhere" was the pitch
  3. Declare a variable with an explicit type, and read a type error
  4. Tell a primitive from a reference, and predict which one == compares
  5. Write, compile and run a Java program from the command line
  6. Explain every word of public static void main(String[] args)
  7. 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.

16A
Compilation, the JVM & Static Types (120 min)
What 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.
16B
Reading Java as a Python Programmer (120 min)
Every word of 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.

PythonJava
Type errors foundWhen the line runsBefore anything runs
Typo in a nameNameError, at runtimeCompile error, immediately
Run a broken programYes — until it reaches the bugNo — it will not compile
Feedback speedInstantOne compile step slower
Refactoring safetyYou find out by testingThe compiler finds most of it
The trade, stated honestly

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)

terminal
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.java

3Types, primitives and references

The distinction Python hides and Java puts in your face.

types.java
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 runs
error: incompatible types: String cannot be converted to int

In 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

PrimitivesReferences
Examplesint, double, boolean, char, long, float, byte, shortString, arrays, every class you write
HoldThe value itselfA pointer to an object
Start as0, 0.0, falsenull
== comparesThe values — what you wantThe addresses — usually not what you want
Spellinglower caseCapitalised
The == trap, previewed

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.

The reassuring part

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.

scratch.pypython not loaded
Values for input(), comma separated →
Output appears here. The first run takes a few seconds while Python loads.

Exercise set

Checked automatically the moment you submit. Work top to bottom — each one assumes the last. Your answers are saved in this browser.

All Warm-up Core Challenge Reset day

Day 17 — Java Control Flow, Arrays & Methods

Familiar structures, unfamiliar rules — including the == trap.

Continue →