You are on page 1of 6

Java bytecode is the instruction set of the Java virtual machine.

Each
bytecode is composed by one, or in some cases two, bytes that represent the
instruction (opcode), along with zero or more bytes for passing parameters.
Of the 2! possible byte"long opcodes, #$% are currently in use, # are
reserved for future use, and & are set aside as permanently unimplemented.
'#()elation to Java'edit(
* Java programmer does not need to be aware of or understand Java
bytecode at all. +owever, as suggested in the ,-. developer/or0s 1ournal,
23nderstanding bytecode and what bytecode is li0ely to be generated by a
Java compiler helps the Java programmer in the same way that 0nowledge of
assembly helps the 4 or 455 programmer.2'2(
,nstructions'edit(
6ee also7 Java bytecode instruction listings
*s each byte has 2! potential values, there are 2! possible opcodes. Of
these, 8988 through 89ca, 89fe, and 89: are assigned values. 89ca is
reserved as a brea0point instruction for Java debuggers and its type is not
used by the language. 6imilarly, 89fe and 89: are not used by the language
and are reserved for internal use by the Java virtual machine.
,nstructions fall into a number of broad groups7
;oad and store (e.g. aload<8, istore)
*rithmetic and logic (e.g. ladd, fcmpl)
=ype conversion (e.g. i2b, d2i)
Ob1ect creation and manipulation (new, put>eld)
Operand stac0 management (e.g. swap, dup2)
4ontrol transfer (e.g. ife?, goto)
.ethod invocation and return (e.g. invo0especial, areturn)
=here are also a few instructions for a number of more specialized tas0s such
as e9ception throwing, synchronization, etc.
.any instructions have pre>9es and@or suA9es referring to the types of
operands they operate on. =hese are as follows7
Bre>9@6uA9 Operand =ype
i integer
l long
s short
b byte
c character
f Coat
d double
z boolean
a reference
Dor e9ample, 2iadd2 will add two integers, while 2dadd2 will add two doubles.
=he 2const2, 2load2, and 2store2 instructions may also ta0e a suA9 of the form
2<n2, where n is a number from 8E& for 2load2 and 2store2. =he ma9imum n for
2const2 di:ers by type.
=he 2const2 instructions push a value of the speci>ed type onto the stac0. Dor
e9ample 2iconst<2 will push an integer , while 2dconst<#2 will push a double
#. =here is also an 2aconst<null2, which pushes 2null2. =he n for the 2load2 and
2store2 instructions speci>es the location in the variable table'clari>cation
needed( to load from or store to. =he 2aload<82 instruction pushes the ob1ect
in variable 8 onto the stac0 (this is usually the 2this2 ob1ect). 2istore<#2 stores
the integer on the top of the stac0 into variable #. Dor variables with higher
numbers the suA9 is dropped and operands must be used.
.odel of computation'edit(
=he model of computation of Java bytecode is that of a stac0"oriented
programming language. Dor e9ample, assembly code for an 9%! processor
might loo0 li0e this7
mov ea9, byte 'ebp"F(
mov ed9, byte 'ebp"%(
add ea9, ed9
mov ec9, ea9
=his code would add two values and move the result to a di:erent location.
6imilar disassembled bytecode might loo0 li0e this7
8 iload<#
# iload<2
2 iadd
& istore<&
+ere, the two values to be added are pushed onto the stac0, where they are
retrieved by the addition instruction, summed, and the result placed bac0 on
the stac0. =he storage instruction then moves the top value of the stac0 into
a variable location. =he numbers in front of the instructions simply represent
the o:set of each instruction from the beginning of the method. =his stac0"
oriented model e9tends to the ob1ect oriented aspects of the language as
well. * method call called 2getGame()2, for e9ample, may loo0 li0e the
following7
.ethod 1ava.lang.6tring getGame()
8 aload<8 @@ =he 2this2 ob1ect is stored in location 8 of the variable table
# get>eld H IDield 1ava.lang.6tring nameJ
@@ =his instruction pops an ob1ect from the top of the stac0,
retrieves the speci>ed
@@ >eld from it, and pushes the >eld onto the stac0.
@@ ,n this e9ample, the 2name2 >eld corresponds to the >fth
constant in the
@@ constant pool of the class.
F areturn @@ )eturns the ob1ect on top of the stac0 from the method.
E9ample'edit(
4onsider the following Java code7
outer7
for (int i K 2L i I #888L i55) M
for (int 1 K 2L 1 I iL 155) M
if (i N 1 KK 8)
continue outerL
O
6ystem.out.println (i)L
O
* Java compiler might translate the Java code above into byte code as
follows, assuming the above was put in a method7
87 iconst<2
#7 istore<#
27 iload<#
&7 sipush #888
!7 if<icmpge FF
$7 iconst<2
#87 istore<2
##7 iload<2
#27 iload<#
#&7 if<icmpge &#
#!7 iload<#
#P7 iload<2
#%7 irem
#$7 ifne 2
227 goto &%
27 iinc 2, #
2%7 goto ##
&#7 getstatic H%FL @@ Dield 1ava@lang@6ystem.out7;1ava@io@Brint6treamL
&F7 iload<#
&7 invo0evirtual H%L @@ .ethod 1ava@io@Brint6tream.println7(,)Q
&%7 iinc #, #
F#7 goto 2
FF7 return
Reneration'edit(
.ain article7 ;ist of JQ. languages
=he most common language targeting Java Qirtual .achine by producing Java
bytecode is Java. Originally only one compiler e9isted, the 1avac compiler
from 6un .icrosystems, which compiles Java source code to Java bytecodeL
but because all the speci>cations for Java bytecode are now available, other
parties have supplied compilers that produce Java bytecode. E9amples of
other compilers include7
Ji0es, compiles from Java to Java bytecode (developed by ,-., implemented
in 455)
Espresso, compiles from Java to Java bytecode (Java #.8 only)
R4J, the RG3 4ompiler for Java, compiles from Java to Java bytecodeL it is also
able to compile to native machine code and is available as part of the RG3
4ompiler 4ollection (R44).
6ome pro1ects provide Java assemblers to enable writing Java bytecode by
hand. *ssembly code may be also generated by machine, for e9ample by a
compiler targeting a Java virtual machine. Gotable Java assemblers include7
Jasmin, ta0es te9tual descriptions for Java classes, written in a simple
assembly"li0e synta9 using Java Qirtual .achine instruction set and generates
a Java class >le '&(
Jamaica, a macro assembly language for the Java virtual machine. Java
synta9 is used for class or interface de>nition. .ethod bodies are speci>ed
using bytecode instructions.'F(
Others have developed compilers, for di:erent programming languages, in
order to target the Java virtual machine, such as7
4oldDusion
J)uby and Jython, two scripting languages based on )uby and Bython
Rroovy, a scripting language based on Java
6cala, a type"safe general"purpose programming language supporting ob1ect"
oriented and functional programming
JRG*= and *pplet.agic, compile from the *da programming language to Java
bytecode
4 to Java byte"code compilers
4lo1ure, a functional, immutable, general"purpose programming language in
the ;,6B family with a strong emphasis on concurrency
.,SletBascal
JavaDT 6cript code is also compiled to Java bytecode.
E9ecution'edit(
=here are several machines available today, both free and commercial
products.
Durther information7 Java virtual machine
,f e9ecuting Java bytecode in a Java virtual machine is not desirable, a
developer can also compile Java source code or Java bytecode directly to
native machine code with tools such as the RG3 4ompiler for Java. 6ome
processors can e9ecute Java bytecode natively. 6uch processors are 0nown as
Java processors.

You might also like