Home      Discussion      Topics      Dictionary      Almanac
Signup       Login
Java bytecode

Java bytecode

Overview
Java bytecode is the form of instructions that the Java virtual machine
Java Virtual Machine
A Java Virtual Machine is a set of computer software programs and data structures that use a virtual machine model for the execution of other computer programs and scripts. The model used by a JVM accepts a form of computer intermediate language commonly referred to as Java bytecode...

 executes. Each bytecode
Bytecode
Bytecode 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 technology, 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. In fact, Sun Microsystems
Sun Microsystems
Sun Microsystems, Inc. is a multinational vendor of computers, computer components, computer software, and information technology services, 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 set of computer software programs and data structures that use a virtual machine model for the execution of other computer programs and scripts. The model used by a JVM accepts a form of computer intermediate language commonly referred to as Java bytecode...

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

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.
Discussion
Ask a question about 'Java bytecode'
Start a new discussion about 'Java bytecode'
Answer questions from other users
Full Discussion Forum
 
Encyclopedia
Java bytecode is the form of instructions that the Java virtual machine
Java Virtual Machine
A Java Virtual Machine is a set of computer software programs and data structures that use a virtual machine model for the execution of other computer programs and scripts. The model used by a JVM accepts a form of computer intermediate language commonly referred to as Java bytecode...

 executes. Each bytecode
Bytecode
Bytecode 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 technology, 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. In fact, Sun Microsystems
Sun Microsystems
Sun Microsystems, Inc. is a multinational vendor of computers, computer components, computer software, and information technology services, 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 set of computer software programs and data structures that use a virtual machine model for the execution of other computer programs and scripts. The model used by a JVM accepts a form of computer intermediate language commonly referred to as Java bytecode...

 and other components of the Java Runtime Environment (JRE), have set aside 3 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, abbreviated IBM, is a multinational computer technology and IT consulting corporation headquartered in Armonk, Town of North Castle, New York, United States. The company is one of the few information technology companies with a continuous history dating...

 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 are Java class files containing platform-neutral Java bytecode...

 helps the Java programmer in the same way that knowledge of assembler
Assembly language
Assembly languages are a family of low-level languages for programming computers, microprocessors, microcontrollers, and other integrated circuits. They implement a symbolic representation of the numeric machine codes and other constants needed to program a particular CPU architecture...

 helps the C
C (programming language)
C is a general-purpose computer programming language developed in 1972 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 a middle-level language, as it comprises a combination of both high-level and low-level language features...

 programmer.".

Instructions


As each byte has 256 potential values, there are 256 possible opcodes. Of these, 0x00 through 0xca, 0xfe, and 0xff are assigned values. 0xba is unused for historical reasons. 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 "i", "l", "s", "b", "c", "f", "d", and "a", standing for, respectively, "integer", "long", "short", "byte", "character", "float", "double", and "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.

Computational model


The computational model 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 and PostScript, and also many Assembly languages ....

. For example, assembly code
Assembly language
Assembly languages are a family of low-level languages for programming computers, microprocessors, microcontrollers, and other integrated circuits. They implement a symbolic representation of the numeric machine codes and other constants needed to program a particular 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 instructon 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 is the fifth field 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.printlnV
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 set of computer software programs and data structures that use a virtual machine model for the execution of other computer programs and scripts. The model used by a JVM accepts a form of computer intermediate language commonly referred to as Java bytecode...

 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 Sun Microsystems.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 and Philippe Charles at IBM but was quickly transformed into an open source project contributed to by an active community of developers. Initially hosted by IBM, the project...

    , compiles from Java to Java bytecode (developed by IBM
    IBM
    International Business Machines Corporation, abbreviated IBM, is a multinational computer technology and IT consulting corporation headquartered in Armonk, Town of North Castle, New York, United States. The company is one of the few information technology companies with a continuous history dating...

    , implemented in C++
    C++
    C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level and low-level language features...

    )
  • 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. Assembler code may be also generated by machine, for example by compiler targeting Java virtual machine
Java Virtual Machine
A Java Virtual Machine is a set of computer software programs and data structures that use a virtual machine model for the execution of other computer programs and scripts. The model used by a JVM accepts a form of computer intermediate language commonly referred to as Java bytecode...

. 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...

    , takes textual descriptions for Java classes, written in a simple assembler-like syntax using Java Virtual Machine instruction set and generates a Java class file
  • Jamaica, a macro assembly language
    Assembly language
    Assembly languages are a family of low-level languages for programming computers, microprocessors, microcontrollers, and other integrated circuits. They implement a symbolic representation of the numeric machine codes and other constants needed to program a particular CPU architecture...

     for the Java virtual machine
    Java Virtual Machine
    A Java Virtual Machine is a set of computer software programs and data structures that use a virtual machine model for the execution of other computer programs and scripts. The model used by a JVM accepts a form of computer intermediate language commonly referred to as Java bytecode...

    . 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 software applications. "Scripts" are distinct from the core code of the application, which is usually written in a different language, and are often created or at least modified...

    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 initially developed and designed by Yukihiro "Matz" Matsumoto...

     and Python
    Python (programming language)
    Python is a general-purpose high-level programming language. Its 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 software applications. "Scripts" are distinct from the core code of the application, which is usually written in a different language, and are often created or at least modified...

     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 is a modern dialect of the Lisp programming language. It is a general-purpose language supporting interactive development that encourages a functional programming style which enables simplified multithreaded programming. Clojure runs on the Java Virtual Machine...


Execution
Java bytecode is designed to be executed in a Java virtual machine
Java Virtual Machine
A Java Virtual Machine is a set of computer software programs and data structures that use a virtual machine model for the execution of other computer programs and scripts. The model used by a JVM accepts a form of computer intermediate language commonly referred to as Java bytecode...

. 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 ARM processors have the ability to execute bytecode directly (see Jazelle
Jazelle
Jazelle DBX allows some ARM processors to execute Java bytecode in hardware as a third execution state alongside the existing ARM and Thumb modes. Jazelle functionality was specified in the ARMv5TEJ architecture and the first processor with Jazelle technology was the ARM926EJ-S...

).
Support for dynamic languages
The Java Virtual Machine
Java Virtual Machine
A Java Virtual Machine is a set of computer software programs and data structures that use a virtual machine model for the execution of other computer programs and scripts. The model used by a JVM accepts a form of computer intermediate language commonly referred to as Java bytecode...

 has currently no built-in support for dynamically typed languages, because the existing JVM instruction set is statically typed - in the sense that method calls have their signatures type-checked at compile time, without a mechanism to defer this decision to run time, or to choose the method dispatch by an alternative approach.

JSR 292
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....

 (Supporting Dynamically Typed Languages on the JavaTM Platform) propose to add 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.
See also

  • 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
  • ARM9E
    ARM9E
    ARM9 is an ARM architecture 32-bit RISC CPU family. With this design generation, ARM moved from a von Neumann architecture to a Modified Harvard architecture with separate instruction and data busses , significantly increasing its potential speed...

  • 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. This makes it very fast at executing Java code while still maintaining the benefits of programming in a high-level language like...

  • Common Intermediate Language
    Common Intermediate Language
    Common Intermediate Language is the lowest-level human-readable programming language in the Common Language Infrastructure and in the .NET Framework. Languages which target the .NET Framework compile to CIL, which is assembled into bytecode...


External links