Enter the JVM: Part 1
After deciding to learn about the JVM, I started reading the JVM specification. I felt the need for trying out few things on my machine to get some practical idea about what the specification tries to tell.
I am currently reading the Chapter 2: The Structure of the Java Virtual Machine. Here is what I got to learn.
Java Virtual Machine Specification
The first line states
This document specifies an abstract machine. It does not describe any particular implementation of the Java Virtual Machine.
This means that the entire specification is an abstract and anyone could implement their own Java Virtual Machine in their preferred programming language. I also googled to see for an already existing implementations of the JVM and found this list.
I think the most widely used JVM implementation is the Hotspot, which is distributed and maintained by Oracle. Just to show the interesting flavours that are possible, here is an implementation of JVM written in Go - https://github.com/zxh0/jvm.go (Thought of doing this when I learnt that JVM is an abstract machine and could be implemented on our own. But was surprised to discover this.)
The specification also tells that it does not mandate few details and leave them upto the choice of the implementors, like
- Garbage collection algorithm used in the JVM.
- Memory layout of run-time data areas (not really sure of what this is)
- Any internal optimization of the Java Virtual Machine instructions
Now, that we know some scope of what to expect, I moving forward.
class file format
The class
file format seems to be pretty big deal.
Compiled code to be executed by the Java Virtual Machine is represented using a hardware- and operating system-independent binary format, typically (but not necessarily) stored in a file, known as the class file format. The class file format precisely defines the representation of a class or interface, including details such as byte ordering that might be taken for granted in a platform-specific object file format.
I used to do Java long time back (mostly in college) and I vividly remember using javac
and java
. The javac
stands for java compiler. We input a java source code file into javac compiler and it produces a .class
file, which we give as input to java
program, which will then run our code. It is good that I remember this much.
1 | $ man javac |
1 | $ man java |
At this point, I am curious to note the contents of a simple .class
file. So, let me write a Hello world program in java and use javac
to compile the source to a .class
file.
1 | // Hello.java |
After doing javac Hello.java
, I got a file named Hello.class
in the same directory. The contents of the file are
1 | ���� 5 |