A hot loop: same answer, interpreted or compiled
square is called a million times, so the JIT compiles it. Run it locally with -Xint and with -XX:+PrintCompilation: the output line never changes.
javac turns source into bytecode; the java launcher starts a JVM that loads classes only when first needed, verifies and links them, runs their static initialisation, then interprets the bytecode while counting what runs most. Hot methods are compiled to optimised machine code by the JIT compilers (C1, then C2), and the JVM can throw that code away and fall back if its guesses turn out wrong.
Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write a program with a class Logger whose static block prints Logger ready, and a static method log(String). Call log twice from main and check Logger ready prints only once.
Predict, then verify: a class Holder has static final String NAME = "box"; and a static block printing Holder init. main prints Holder.NAME. What is printed?
Write a program that sums the cubes of 1 to 1,000 with a helper method cube(long n), and prints the total. Then explain why the result is the same with -Xint.
Explain it without notes
Describe what happens, step by step, from typing java Main to the first line of output.
What are the three built-in class loaders, and what is parent delegation for?
When exactly does a class's static initialiser run? Give two things that do not trigger it.
What is tiered compilation, and what are C1 and C2?
What is deoptimisation, and why does the JVM need it?
Expected output
Total: 333332833333500000