Lecture 3 JRE, Class Format
Lecture 3 JRE, Class Format
CIC-212
Java Programming
Lecture 3
2
Difference between JDK/JRE/JVM/JIT
JVM becomes an instance of JRE
at runtime of a java program. It is
widely known as a runtime
interpreter. The Java virtual
machine (JVM) is the cornerstone
on top of which the Java
technology is built upon. It is the
component of the Java technology
responsible for its hardware and
platform independence. JVM
largely helps in the abstraction of
inner implementation from the
programmers who make use of
libraries for their programmes from
JDK.
3
Difference between JDK/JRE/JVM/JIT
JVM Internals
Like a real computing machine, JVM has an
instruction set and manipulates various memory
areas at run time. Thus for different hardware
platforms one has corresponding implementation
of JVM available as vendor supplied JREs. It is
common to implement a programming language
using a virtual machine.
4
Difference between JDK/JRE/JVM/JIT
5
Java .class File Structure
We saw some basic of internals of JVM and
how it is divided into different components
that helps in execution of Java byte code.
To Remember:
My Very Cute Animal Turns Savage In Full Moon Areas.
Java .class File Structure
• The length of the Java class is not known before
it gets loaded. There are variable length sections
such as constant pool, methods, attributes etc.
These sections are organized in such a way that
they are prefaced by their size or length. This way
JVM knows the size of variable length sections
before actually loading them.
The constants are stored as a variable length array element in the Constant
pool. The arrays of constants are preceded by its array size, hence JVM knows
how many constants it will expect while loading the class file. In above
diagram, the portion represented in green contains the size of the array.
Java .class File Structure
3. Constant Pool
Within each array elements first byte represents a tag
specifying the type of constant at that position in the
array.
In below diagram the portion in orange represent the
one-byte tag. JVM identifies the type of the constant
by reading one-byte tag. Hence if one-byte tag
represents a String literal then JVM knows that next
2 bytes represents length of the String literal and rest
of the entry is string literal itself.
Java descriptor
Boolean Z
integer I
Object Ljava/lang/Object;
String[] Ljava/lang/String;
int foo(int,Object)
(ILjava/lang/Object;)I
Java .class File Structure
3. Constant Pool
We can analyse the Constant Pool of any class file using javap command. Executing
javap on above Main class, we get following symbol table.
7. Interfaces
All the interfaces that are implemented by the class
(or interface) defined in the file goes in Interface
section of a class file. Starting two byte of the
Interface section is the count that provides
information about total number of interfaces being
implemented. Immediately following is an array that
contains one index into the constant pool for each
interface implemented by class.
Java .class File Structure
8. Fields
A field is an instance or a class level variable
(property) of the class or interface. Fields section
contains only those fields that are defined by the
class or an interface of the file and not those fields
which are inherited from the super class or super
interface.
First two bytes in Fields section represents count:
that is the total number of fields in Fields Section.
Following the count is an array of variable length
structure one for each field. Each element in this
array represent one field. Some information is stored
in this structure where as some information like
name of the fields are stored in Constant pool.
Java .class File Structure
9. Methods
The Methods component host the methods that are
explicitly defined by this class, not any other
methods that may be inherited from super class.
First two byte is the count of the number of methods
in the class or interface. The rest is again a variable
length array which holds each method structure.
Method structure contains several pieces of
information about the method like method argument
list, its return type, the number of stack words
required for the method’s local variables, stack
words required for method’s operand stack, a table
for exceptions, byte code sequence etc.
Java .class File Structure
10. Attributes
Attribute section contains several attribute about the
class file, e.g. one of the attribute is the source code
attribute which reveals the name of the source file
from which this class file was compiled.
First two bytes in Attribute section is count of the
number of attributes, followed by the attributes
themselves. The JVMs will ignore any attributes they
don’t understand.