JVM internals for Kotlin and Android developers · check

Know Loading and linking code already?

One question for each of the 8 puzzles in this module. A right answer marks that puzzle as passed, so you can jump ahead. A wrong one tells you which puzzle to start with. Nothing is sent anywhere: it's saved in this browser.

Puzzle 11: When does a class arrive?

You delete both Rare.value lines but keep new Rare[2]. What does the program print?

Not quite. Nothing fails; Rare is loaded and never needs initializing. Learn it in puzzle 11 →

Not quite. Creating an array of Rares loads Rare but runs none of its code. Learn it in puzzle 11 →

Right. Loading isn't initialization.

Puzzle 12: Why is it zero?

In Settings, you add a third static line after limit: val tripled = limit * 3. What does tripled hold?

Right. Order matters line by line.

Not quite. One thread runs the static code, top to bottom. Learn it in puzzle 12 →

Not quite. Only reads before the writing line see defaults; limit is 10 by then. Learn it in puzzle 12 →

Puzzle 13: Why does the second try say something different?

Prices' static code is fixed, but a class Rates has static int r = 1 / 0, which throws as soon as it runs. main reads Rates.r three times, catching each time. What does the third attempt get?

Not quite. Only the first attempt runs the static code; after that Rates is marked erroneous. Learn it in puzzle 13 →

Right. Every use after the first failure gets NoClassDefFoundError.

Not quite. A class marked erroneous can't be used at all, so the field is never read. Learn it in puzzle 13 →

Puzzle 14: Why can't Greeter be cast to Greeter?

You write your own class named java.lang.String and put it on the class path. Which String does your code get?

Not quite. Only one loader ever gets to define it: bootstrap answers first. Learn it in puzzle 14 →

Not quite. The application loader asks its parents before it looks at the class path. Learn it in puzzle 14 →

Right. Parent delegation protects the core classes.

Puzzle 15: Not found, or no longer there?

A server crashes with NoClassDefFoundError: com/example/Json, caused by ClassNotFoundException. The code calls Json.parse directly. What's the most likely cause?

Not quite. A direct call compiles to bytecode referring to Json; no string name is involved. Learn it in puzzle 15 →

Right. Compiled code depended on a class that isn't deployed.

Not quite. That message says "Could not initialize class" instead of being caused by ClassNotFoundException. Learn it in puzzle 15 →

Puzzle 16: Can the JVM check before main runs?

The module app requires bonus, and bonus requires a third module, json, which is missing. App's main doesn't touch json at all. Does start print?

Right. Startup resolution covers every module reachable through requires.

Not quite. That is the class path's behavior; the module path checks first. Learn it in puzzle 16 →

Not quite. Resolving follows each required module's own requires lines too. Learn it in puzzle 16 →

Puzzle 17: Where do classes live, and how do they leak?

The list keeps the loader object instead of the Greeter. What happens to the run?

Not quite. A class is unloaded only with its loader, and the list reaches the loader. Learn it in puzzle 17 →

Right. Reaching the loader is enough.

Not quite. The failure comes from class metadata kept in metaspace, not from the size of what's in the list. Learn it in puzzle 17 →

Puzzle 18: What does Android do with all this?

You unzip your release APK and search it for Bookmark.class, which kotlinc wrote during the build. What do you find?

Not quite. PathClassLoader loads classes from DEX; the APK carries no class files. Learn it in puzzle 18 →

Right. d8 converts every class file into DEX before packaging.

Not quite. d8 runs in every build; it is how class files become DEX. Learn it in puzzle 18 →

Back to the overview →