CodeBlanks Logo Icon CodeBlanks

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.

Practice 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.

Starter code

Main.java
public class Main {
    public static void main(String[] args) {
        // Print the Java version, thread name and String loader
    }
}

Sample solution

Main.java
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());
    }
}

Expected output

21
main
null

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.

Practice 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().

Starter code

Main.java
public class Summary {
    // Define an int method (parameter n)

    public static void main(String[] args) {
        // Call the method with 21 and print the Java version
    }
}

Sample solution

Main.java
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());
    }
}

Expected output

42
21

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 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 Correct answer

Explanation: The JIT compiles hot methods into machine code.

2 Match the JVM summary

Term Meaning
Runtime.version() Returns the Java version
String.class.getClassLoader() Returns null
Thread.currentThread() Current thread
javap -c Shows bytecode

All lessons in Expert