This chapter covers
- Introducing JVM internals
- Reflection Internals
- Method Handles
- Invokedynamic
- Recent Internal Changes
- Unsafe
Java’s virtual machine is an extremely sophisticated runtime environment that has, for decades, prioritized stability and production-grade engineering. For these reasons, many Java developers have never needed to poke about in the internals - because it is simply not necessary to do so most of the time.
This chapter, on the other hand, is for the curious; the people who would like to know more; who would like to draw back the curtain and see some of the details of how the JVM is implemented. Let’s start with method invocation.
To get going, let’s look at a simple example, defined by the classes Pet
, Cat
and Bear
and the interface Furry
. This can be seen in Figure 17.1
We might also suppose that other subclasses of Pet
exist (for example Dog
and Fish
) that aren’t shown in the diagram in order to keep things clear. We’ll use this example to explain in detail how the different invoke opcodes work, starting with invokevirtual
.
The most common type of method invocation is calling an instance method on an object of a specific class (or a subclass) by use of an invokevirtual
bytecode.