Java
Summary - JVM internals
Combine class loaders, bytecode and JIT to write a JVM inspection tool.
Summary - JVM internals
In this group you saw the JVM architecture, the class loader, bytecode, the JIT compiler and the runtime data areas. The JVM consists of a class loader, data areas and an execution engine.
Now you will combine them into a mini task: define a method, print its result, and on the same code print the Java version and the main thread name. This shows how you observe the JVM at runtime.
Exercise Print JVM info
Define a Main class. In main, print three lines: (1) Runtime.version().feature(), (2) Thread.currentThread().getName(), (3) String.class.getClassLoader(). Each on a separate System.out.println call.
Show example solution
public class Main {
public static void main(String[] args) {
System.out.println(Runtime.version().feature());
System.out.println(Thread.currentThread().getName());
System.out.println(String.class.getClassLoader());
}
}
What comes next?
You learned the core JVM mechanisms. In the next group you will examine the memory model, the happens-before relationship, the guarantees of volatile and final fields, and the Metaspace region.
This group combined JVM architecture, the class loader, bytecode, the JIT and the data areas. Next you will explore concurrency visibility and ordering rules.
Exercise Summary: method and version
Define a new Summary class. Add an int double_it(int n) method that returns n * 2. In main, call double_it(21) and print the result. Also print Runtime.version().feature().
Show example solution
public class Summary {
int double_it(int n) {
return n * 2;
}
public static void main(String[] args) {
Summary s = new Summary();
System.out.println(s.double_it(21));
System.out.println(Runtime.version().feature());
}
}
Test yourself
-
What does the JIT do when a method is called often?
- It deletes the method
- It stops interpreting the method
- It moves the method to another class
- It compiles the method to machine code
Show answer
It compiles the method to machine code — The JIT compiles hot methods into machine code.
-
Match the JVM summary
- Runtime.version()
- Returns the Java version
- String.class.getClassLoader()
- Returns null
- Thread.currentThread()
- Current thread
- javap -c
- Shows bytecode
Next lesson: Heap and stack