Java bytecode
Encyclopedia
Java bytecode is the form of instructions that the Java virtual machine
Java Virtual Machine
A Java virtual machine is a virtual machine capable of executing Java bytecode. It is the code execution component of the Java software platform. Sun Microsystems stated that there are over 4.5 billion JVM-enabled devices.-Overview:...

 executes. Each bytecode
Bytecode
Bytecode, also known as p-code , is a term which has been used to denote various forms of instruction sets designed for efficient execution by a software interpreter as well as being suitable for further compilation into machine code...

 opcode
Opcode
In computer science engineering, an opcode is the portion of a machine language instruction that specifies the operation to be performed. Their specification and format are laid out in the instruction set architecture of the processor in question...

 is one byte in length, although some require parameters, resulting in some multi-byte instructions. Not all of the possible 256 opcodes are used. 51 are reserved for future use. Beyond that, Sun Microsystems
Sun Microsystems
Sun Microsystems, Inc. was a company that sold :computers, computer components, :computer software, and :information technology services. Sun was founded on February 24, 1982...

, the original creators of the Java programming language
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

, the Java virtual machine
Java Virtual Machine
A Java virtual machine is a virtual machine capable of executing Java bytecode. It is the code execution component of the Java software platform. Sun Microsystems stated that there are over 4.5 billion JVM-enabled devices.-Overview:...

 and other components of the Java Runtime Environment (JRE), have set aside three values to be permanently unimplemented.

Relation to Java

A Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

 programmer does not need to be aware of or understand Java bytecode at all. However, as suggested in the IBM
IBM
International Business Machines Corporation or IBM is an American multinational technology and consulting corporation headquartered in Armonk, New York, United States. IBM manufactures and sells computer hardware and software, and it offers infrastructure, hosting and consulting services in areas...

 developerWorks journal, "Understanding bytecode and what bytecode is likely to be generated by a Java compiler
Java compiler
A Java compiler is a compiler for the Java programming language. The most common form of output from a Java compiler is Java class files containing platform-neutral Java bytecode...

 helps the Java programmer in the same way that knowledge of assembler
Assembly language
An assembly language is a low-level programming language for computers, microprocessors, microcontrollers, and other programmable devices. It implements a symbolic representation of the machine codes and other constants needed to program a given CPU architecture...

 helps the C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

 or C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

 programmer."

Instructions

See also: Java bytecode instruction listings
Java bytecode instruction listings
This is a list of the instructions that make up the Java bytecode, an abstract machine language that is ultimately executed by the Java virtual machine...



As each byte has 256 potential values, there are 256 possible opcodes. Of these, 0x00 through 0xca, 0xfe, and 0xff are assigned values. 0xca is reserved as a breakpoint instruction for debuggers and is not used by the language. Similarly, 0xfe and 0xff are not used by the language, and are reserved for internal use by the virtual machine.

Instructions fall into a number of broad groups:
  • Load and store (e.g. aload_0,istore)
  • Arithmetic and logic (e.g. ladd,fcmpl)
  • Type conversion (e.g. i2b,d2i)
  • Object creation and manipulation (new,putfield)
  • Operand stack management (e.g. swap,dup2)
  • Control transfer (e.g. ifeq,goto)
  • Method invocation and return (e.g. invokespecial,areturn)


There are also a few instructions for a number of more specialized tasks such as exception throwing, synchronization, etc.

Many instructions have prefixes and/or suffixes referring to the types of operands they operate on. These are as follows:
Prefix/Suffix Operand Type
i integer
l long
s short
b byte
c character
f float
d double
a reference


For example, "iadd" will add two integers, while "dadd" will add two doubles. The "const", "load", and "store" instructions may also take a suffix of the form "_n", where n is a number from 0-3 for "load" and "store". The maximum n for "const" differs by type.

The "const" instructions push a value of the specified type onto the stack. For example "iconst_5" will push an integer 5, while "dconst_1" will push a double 1. There is also an "aconst_null", which pushes "null". The n for the "load" and "store" instructions specifies the location in the variable table to load from or store to. The "aload_0" instruction pushes the object in variable 0 onto the stack (this is usually the "this" object). "istore_1" stores the integer on the top of the stack into variable 1. For variables with higher numbers the suffix is dropped and operators must be used .

Model of computation

The model of computation
Model of computation
In computability theory and computational complexity theory, a model of computation is the definition of the set of allowable operations used in computation and their respective costs...

 of Java bytecode is that of a stack-oriented programming language
Stack-oriented programming language
A stack-oriented programming language is one that relies on a stack machine model for passing parameters. Several programming languages fit this description, notably Forth, RPL, PostScript, and also many Assembly languages ....

. For example, assembly code
Assembly language
An assembly language is a low-level programming language for computers, microprocessors, microcontrollers, and other programmable devices. It implements a symbolic representation of the machine codes and other constants needed to program a given CPU architecture...

 for an x86 processor might look like this:

add eax, edx
mov ecx, eax
This code would add two values and move the result to a different location. Similar disassembled bytecode might look like this:

0 iload_1
1 iload_2
2 iadd
3 istore_3

Here, the two values to be added are pushed onto the stack, where they are retrieved by the addition instruction, summed, and the result placed back on the stack. The storage instruction then moves the top value of the stack into a variable location. The numbers in front of the instructions simply represent the offset of each instruction from the beginning of the method.
This stack-oriented model extends to the object oriented aspects of the language as well. A method call called "getName", for example, may look like the following:

Method java.lang.String getName
0 aload_0 // The "this" object is stored in location 0 of the variable table
1 getfield #5
// This instruction pops an object from the top of the stack, retrieves the specified
// field from it, and pushes the field onto the stack.
// In this example, the "name" field corresponds to the fifth constant in the
// constant pool of the class.
4 areturn // Returns the object on top of the stack from the method.

Example

Consider the following Java code:

outer:
for (int i = 2; i < 1000; i++) {
for (int j = 2; j < i; j++) {
if (i % j 0)
continue outer;
}
System.out.println (i);
}


A Java compiler might translate the Java code above into byte code as follows, assuming the above was put in a method:

0: iconst_2
1: istore_1
2: iload_1
3: sipush 1000
6: if_icmpge 44
9: iconst_2
10: istore_2
11: iload_2
12: iload_1
13: if_icmpge 31
16: iload_1
17: iload_2
18: irem
19: ifne 25
22: goto 38
25: iinc 2, 1
28: goto 11
31: getstatic #84; //Field java/lang/System.out:Ljava/io/PrintStream;
34: iload_1
35: invokevirtual #85; //Method java/io/PrintStream.println:(I)V
38: iinc 1, 1
41: goto 2
44: return

Generation
The most common language targeting Java Virtual Machine
Java Virtual Machine
A Java virtual machine is a virtual machine capable of executing Java bytecode. It is the code execution component of the Java software platform. Sun Microsystems stated that there are over 4.5 billion JVM-enabled devices.-Overview:...

 by producing Java bytecode is Java. Originally only one compiler existed, the javac
Javac
javac is the primary Java compiler, included in the Java Development Kit from Oracle Corporation.The compiler accepts source code conforming to the Java language specification and produces bytecode conforming to the Java Virtual Machine Specification .javac is itself written in Java...

 compiler from Sun Microsystems, which compiles Java source code to Java bytecode; but because all the specifications for Java bytecode are now available, other parties have supplied compilers that produce Java bytecode. Examples of other compilers include:
  • Jikes
    Jikes
    Jikes is an open source Java compiler written in C++. It is no longer being updated.The original version was developed by David L. "Dave" Shields and Philippe Charles at IBM but was quickly transformed into an open source project contributed to by an active community of developers. Initially hosted...

    , compiles from Java to Java bytecode (developed by IBM
    IBM
    International Business Machines Corporation or IBM is an American multinational technology and consulting corporation headquartered in Armonk, New York, United States. IBM manufactures and sells computer hardware and software, and it offers infrastructure, hosting and consulting services in areas...

    , implemented in C++
    C++
    C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

    )
  • Espresso, compiles from Java to Java bytecode (Java 1.0 only)
  • GCJ, the GNU Compiler for Java, compiles from Java to Java bytecode; it is also able to compile to native machine code and is available as part of the GNU Compiler Collection (GCC)
    GNU Compiler Collection
    The GNU Compiler Collection is a compiler system produced by the GNU Project supporting various programming languages. GCC is a key component of the GNU toolchain...

    .


Some projects provide Java assemblers to enable writing Java bytecode by hand. Assembly code may be also generated by machine, for example by compiler targeting Java virtual machine
Java Virtual Machine
A Java virtual machine is a virtual machine capable of executing Java bytecode. It is the code execution component of the Java software platform. Sun Microsystems stated that there are over 4.5 billion JVM-enabled devices.-Overview:...

. Notable Java assemblers include:
  • Jasmin
    Jasmin (Java assembler)
    Jasmin is a free open source assembler to create class files from human readable assembler-like syntax using the Java Virtual Machine instruction sets. Jasmin is not a Java programming language compiler....

    , takes textual descriptions for Java classes, written in a simple assembly-like syntax using Java Virtual Machine instruction set and generates a Java class file
  • Jamaica, a macro assembly language
    Assembly language
    An assembly language is a low-level programming language for computers, microprocessors, microcontrollers, and other programmable devices. It implements a symbolic representation of the machine codes and other constants needed to program a given CPU architecture...

     for the Java virtual machine
    Java Virtual Machine
    A Java virtual machine is a virtual machine capable of executing Java bytecode. It is the code execution component of the Java software platform. Sun Microsystems stated that there are over 4.5 billion JVM-enabled devices.-Overview:...

    . Java syntax is used for class or interface definition. Method bodies are specified using bytecode instructions.


Others have developed compilers, for different programming languages, in order to target the Java virtual machine, such as:
  • JRuby
    JRuby
    JRuby is a Java implementation of the Ruby programming language, being developed by the JRuby team. It is free software released under a three-way CPL/GPL/LGPL license...

     and Jython
    Jython
    Jython, successor of JPython, is an implementation of the Python programming language written in Java.-Overview:Jython programs can seamlessly import and use any Java class. Except for some standard modules, Jython programs use Java classes instead of Python modules...

    , two scripting language
    Scripting language
    A scripting language, script language, or extension language is a programming language that allows control of one or more applications. "Scripts" are distinct from the core code of the application, as they are usually written in a different language and are often created or at least modified by the...

    s based on Ruby
    Ruby (programming language)
    Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...

     and Python
    Python (programming language)
    Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...

  • Groovy, a scripting language
    Scripting language
    A scripting language, script language, or extension language is a programming language that allows control of one or more applications. "Scripts" are distinct from the core code of the application, as they are usually written in a different language and are often created or at least modified by the...

     based on Java
  • Scala, a type-safe general-purpose programming language supporting object-oriented and functional programming
  • JGNAT and AppletMagic
    AdaMagic
    AdaMagic is a retargetable Ada 95 compiler technology available from SofCheck. AdaMagic includes an ACATS-conformant Ada 95 front end that can generate various intermediate representations, and an ACATS-conformant Ada 95 run-time system that can be hosted on Unix-like operating systems, real-time...

    , compile from the Ada programming language to Java bytecode
  • C to Java byte-code compilers
  • Clojure
    Clojure
    Clojure |closure]]") is a recent dialect of the Lisp programming language created by Rich Hickey. It is a general-purpose language supporting interactive development that encourages a functional programming style, and simplifies multithreaded programming....

  • MIDletPascal
    MIDletPascal
    MIDletPascal is an Pascal compiler and IDE specifically designed to create software for mobiles. It generates Java bytecode that runs on any Java ME device. In September 2009, Niksa Orlic, who wrote MIDlet pascal, transmitted the source code to the Russian Boolean.name development community for...

  • JavaFX Script
    JavaFX Script
    JavaFX Script is a scripting language designed by Sun Microsystems, forming part of the JavaFX family of technologies on the Java Platform.JavaFX targets the Rich Internet Application domain , specializing in rapid development of visually rich applications for the desktop and mobile markets...

     code is also compiled to Java bytecode.

Execution
Java bytecode is designed to be executed in a Java virtual machine
Java Virtual Machine
A Java virtual machine is a virtual machine capable of executing Java bytecode. It is the code execution component of the Java software platform. Sun Microsystems stated that there are over 4.5 billion JVM-enabled devices.-Overview:...

. There are several virtual machines available today, both free and commercial products.
If executing 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 GNU Compiler for Java. Some processors can execute Java bytecode natively. Such processors are known as Java processor
Java processor
A Java processor is the implementation of the Java Virtual Machine in hardware.In other words the bytecodes that make up the instruction set of the abstract machine become the instruction set of a concrete machine.- Implementations :...

s
.
Support for dynamic languages
The Java Virtual Machine
Java Virtual Machine
A Java virtual machine is a virtual machine capable of executing Java bytecode. It is the code execution component of the Java software platform. Sun Microsystems stated that there are over 4.5 billion JVM-enabled devices.-Overview:...

 provides some support for dynamically typed languages. Most of the existing JVM instruction set is statically typed - in the sense that method calls have their signatures type-checked at compile time
Compile time
In computer science, compile time refers to either the operations performed by a compiler , programming language requirements that must be met by source code for it to be successfully compiled , or properties of the program that can be reasoned about at compile time.The operations performed at...

, without a mechanism to defer this decision to run time, or to choose the method dispatch by an alternative approach.

JSR
Java Community Process
The Java Community Process or JCP, established in 1998, is a formalized process that allows interested parties to get involved in the definition of future versions and features of the Java platform....

 292 (Supporting Dynamically Typed Languages on the Java™ Platform) added a new invokedynamic instruction at the JVM level, to allow method invocation relying on dynamic type checking (instead of the existing statically type-checked invokevirtual instruction). The Da Vinci Machine
Da Vinci Machine
The Da Vinci Machine, also called the Multi Language Virtual Machine is a Sun Microsystems project aiming to prototype the extension of the Java Virtual Machine to add support for dynamic languages....

 is a prototype virtual machine implementation that hosts JVM extensions aimed at supporting dynamic languages. All JVMs supporting J2SE
Java Platform, Standard Edition
Java Platform, Standard Edition or Java SE is a widely used platform for programming in the Java language. It is the Java Platform used to deploy portable applications for general use...

 7 also include the invokedynamic opcode.
See also


  • Java bytecode instruction listings
    Java bytecode instruction listings
    This is a list of the instructions that make up the Java bytecode, an abstract machine language that is ultimately executed by the Java virtual machine...

  • Class (file format)
    Class (file format)
    In the Java programming language, source files are compiled into machine-readable class files which have a .class extension. Since Java is a platform-independent language, source code is compiled into an output file known as bytecode, which it stores in a .class file. If a source file has more...

  • List of JVM languages
  • Java backporting tools
  • C to Java Virtual Machine compilers
  • JStik
    Jstik
    The JStik is a microcontroller based on the aJile Systems line of embedded Java processors. It is novel in that it uses Java byte code as the native machine language...

  • Common Intermediate Language
    Common Intermediate Language
    Common Intermediate Language is the lowest-level human-readable programming language defined by the Common Language Infrastructure specification and is used by the .NET Framework and Mono...

    (CIL)

External links
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK