Compilation Process and Java Bytecode: Blue Ocean Research Journals 1
Compilation Process and Java Bytecode: Blue Ocean Research Journals 1
Abstract
High-level languages used for programming like Java,C,C++ etc. Compiles a program to its equelent low level
code which can be understood and executed by the machine. Here we will discuss about the Java compilation
process, i:e, how Java compiles a code and what stages it have in compilation process. We basically focus on
the Java bytecode and its advantages over native code. As we know the purpose of compilation is mainly to
produce the executable version of a program. We also discuss the various approaches of compilation in java.
The first approach is to compiling tha java bytecode, second is the two stages process going through the java
bytecode to java native code, the third approach is to go through bypassing the java bytecode and going directly
to natice machine code.
Keywords: Java Bytecode, Compilation process, Native code,JIT(Just in time), condotions in javabytecode,
methods in java bytecode.
1.
Introduction
Fig. 1
www.borjournals.com
www.borjournals.com
}
First of all compile the file and get the file Foo.class
which is the bytecode file. Open the bytecode file
using hexeditor and you will see some hexcodes like
as shown in fig. 2 below :
www.borjournals.com
}
The class is very simple and it is easy to see the
relation between the sourced code and the generated
bytecode. First of all we notice that in the bytecode
version of the class the compiler inferred the default
constructor (as promised by the JVM spec).
Secondly, if we study the Java bytecode instructions
(in our example aload_0 and aload_1), we can see that
some of the instructions have prefixes like aload_0 or
istore_2. This is related to the type of the data that the
instruction operates with. The prefix 'a' means that the
opcode is manipulating an object reference. The
prefix 'i' means the opcode is manipulating an integer.
3.1 Conditionals in Java bytecode:
Of course different programs, even if they achieve the
same effect, will usually give rise to different
bytecode sequences when compiled. We consider
now two different ways of implementing a method to
compute the absolute value of an integer (the absolute
value of an negative integer n is n whereas the
absolute value of a positive integer n is n itself.
We write two versions of a method to compute
the absolute value of n. These versions are called
absFirst() and absSecond(). The difference between
them is whether we test for being negative or test for
being positive. In the rst case we place the value n
on the true limb of the conditional and th e value n on
false
limb
of
the
conditional.
F
f
fig. 3 These give rise to the following bytecode sequences when compiled
Fig 4
www.borjournals.com
Fig. 5
3.3 Simple method invocation:
The simplest type of method to invoke in Java is a
static method with no parameters. Below we show the
Java source code for a class with tree static methods
and the relevant part of the compiled bytecode for this
class. The methods of the class are referred to by
Fig. 6
Seen from inside the method, formal parameters are
simply numbered, just as local variables are. Thus the methods add2() and add1()
refer to the integer variable
numbered zero (using iload 0).
www.borjournals.com
www.borjournals.com
5. Refrences
[1] http://stackoverflow.com/questions/3406942/how
-exactly-does-java-compilation-take-place
[2] http://www.javaworld.com/jw-09-1996/jw-09bytecodes.html
[3] http://althing.cs.dartmouth.edu/local/www.acm.ui
uc.edu/sigmil/RevEng/ch02.html
[4] http://arhipov.blogspot.in/2011/01/java-bytecodefundamentals.html
[5] The Java Virtual Machine is described in the
book The Java Virtual Machine Specica[6] tion by Tim Lindholm and Frank Yellin,
Addison-Wesley, Second edition, 1999.