Java Bytecode
   HOME

TheInfoList



OR:

In computing, Java bytecode is the
bytecode Bytecode (also called portable code or p-code) is a form of instruction set designed for efficient execution by a software interpreter. Unlike human-readable source code, bytecodes are compact numeric codes, constants, and references (norma ...
-structured
instruction set In computer science, an instruction set architecture (ISA), also called computer architecture, is an abstract model of a computer. A device that executes instructions described by that ISA, such as a central processing unit (CPU), is called an ' ...
of the
Java virtual machine A Java virtual machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode. The JVM is detailed by a specification that formally describes ...
(JVM), a
virtual machine In computing, a virtual machine (VM) is the virtualization/emulation of a computer system. Virtual machines are based on computer architectures and provide functionality of a physical computer. Their implementations may involve specialized hardw ...
that enables a computer to run programs written in the
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
programming language and several other programming languages, see
List of JVM languages This list of JVM Languages comprises notable computer programming languages that are used to produce computer software that runs on the Java virtual machine (JVM). Some of these languages are interpreted by a Java program, and some are compiled ...
.


Relation to Java

A
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
programmer does not need to be aware of or understand Java bytecode at all. However, as suggested in the IBM developerWorks journal, "Understanding bytecode and what bytecode is likely to be generated by a
Java compiler A Java compiler is a compiler for the programming language Java. The most common form of output from a Java compiler is Java class files containing platform-neutral Java bytecode, but there are also compilers that output optimized native machine ...
helps the Java programmer in the same way that knowledge of
assembly Assembly may refer to: Organisations and meetings * Deliberative assembly, a gathering of members who use parliamentary procedure for making decisions * General assembly, an official meeting of the members of an organization or of their representa ...
helps the C or
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
programmer."


Instruction set architecture

The JVM is both a
stack machine In computer science, computer engineering and programming language implementations, a stack machine is a computer processor or a virtual machine in which the primary interaction is moving short-lived temporary values to and from a push down st ...
and a
register machine In mathematical logic and theoretical computer science a register machine is a generic class of abstract machines used in a manner similar to a Turing machine. All the models are Turing equivalent. Overview The register machine gets its name from ...
. Each
frame A frame is often a structural system that supports other components of a physical construction and/or steel frame that limits the construction's extent. Frame and FRAME may also refer to: Physical objects In building construction *Framing (con ...
for a method call has an "operand stack" and an array of "local variables". The operand stack is used for operands to computations and for receiving the return value of a called method, while local variables serve the same purpose as registers and are also used to pass method arguments. The maximum size of the operand stack and local variable array, computed by the compiler, is part of the attributes of each method. Each can be independently sized from 0 to 65535 values, where each value is 32 bits. and types, which are 64 bits, take up two consecutive local variables (which need not be 64-bit aligned in the local variables array) or one value in the operand stack (but are counted as two units in the depth of the stack).


Instruction set

Each
bytecode Bytecode (also called portable code or p-code) is a form of instruction set designed for efficient execution by a software interpreter. Unlike human-readable source code, bytecodes are compact numeric codes, constants, and references (norma ...
is composed of one byte that represents the
opcode In computing, an opcode (abbreviated from operation code, also known as instruction machine code, instruction code, instruction syllable, instruction parcel or opstring) is the portion of a machine language instruction that specifies the operat ...
, along with zero or more bytes for operands. Of the 256 possible byte-long
opcode In computing, an opcode (abbreviated from operation code, also known as instruction machine code, instruction code, instruction syllable, instruction parcel or opstring) is the portion of a machine language instruction that specifies the operat ...
s, , 202 are in use (~79%), 51 are reserved for future use (~20%), and 3 instructions (~1%) are permanently reserved for JVM implementations to use. Two of these (impdep1 and impdep2) are to provide traps for implementation-specific software and hardware, respectively. The third is used for debuggers to implement breakpoints. 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: 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 (32 bit value) with the value 5 onto the stack, while dconst_1 will push a double (64 bit floating point value) with the value 1 onto the stack. There is also an aconst_null, which pushes a reference. The ''n'' for the load and store instructions specifies the index in the local variable array to load from or store to. The aload_0 instruction pushes the object in local variable 0 onto the stack (this is usually the
this This may refer to: * ''This'', the singular proximal demonstrative pronoun Places * This, or ''Thinis'', an ancient city in Upper Egypt * This, Ardennes, a commune in France People with the surname * Hervé This, French culinary chemist Arts, e ...
object). istore_1 stores the integer on the top of the stack into local variable 1. For local variables beyond 3 the suffix is dropped and operands must be used.


Example

Consider the following Java code: outer: for (int i = 2; i < 1000; i++) A Java compiler might translate the Java code above into bytecode 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 A Java virtual machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode. The JVM is detailed by a specification that formally describes ...
by producing Java bytecode is Java. Originally only one compiler existed, the
javac javac (pronounced "java-see") is the primary Java compiler included in the Java Development Kit (JDK) from Oracle Corporation. Martin Odersky implemented the GJ compiler, and his implementation became the basis for javac. The compiler accepts s ...
compiler from
Sun Microsystems Sun Microsystems, Inc. (Sun for short) was an American technology company that sold computers, computer components, software, and information technology services and created the Java programming language, the Solaris operating system, ZFS, the ...
, which compiles
Java source code Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let programmers ''write once, run any ...
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: *Eclipse compiler for Java (ECJ) *
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 ...
, compiles from Java to Java bytecode (developed by IBM, implemented in
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
) *Espresso, compiles from Java to Java bytecode (Java 1.0 only) *
GNU Compiler for Java The GNU Compiler for Java (GCJ) is a free compiler for the Java programming language. It was part of the GNU Compiler Collection for over ten years but as of 2017 it is no longer maintained and will not be part of future releases. GCJ compiles ...
(GCJ), compiles from Java to Java bytecode; it can also compile to native
machine code In computer programming, machine code is any low-level programming language, consisting of machine language instructions, which are used to control a computer's central processing unit (CPU). Each instruction causes the CPU to perform a very ...
and was part of the
GNU Compiler Collection The GNU Compiler Collection (GCC) is an optimizing compiler produced by the GNU Project supporting various programming languages, hardware architectures and operating systems. The Free Software Foundation (FSF) distributes GCC as free software ...
(GCC) up until version 6. Some projects provide Java assemblers to enable writing Java bytecode by hand. Assembly code may be also generated by machine, for example by a compiler targeting a
Java virtual machine A Java virtual machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode. The JVM is detailed by a specification that formally describes ...
. Notable Java assemblers include: * Jasmin, takes text 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 In computer programming, assembly language (or assembler language, or symbolic machine code), often referred to simply as Assembly and commonly abbreviated as ASM or asm, is any low-level programming language with a very strong correspondence be ...
for the
Java virtual machine A Java virtual machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode. The JVM is detailed by a specification that formally describes ...
. Java syntax is used for class or interface definition. Method bodies are specified using bytecode instructions. *Krakatau Bytecode Tools, currently contains three tools: a decompiler and disassembler for Java classfiles and an assembler to create classfiles. *Lilac, an assembler and disassembler for the
Java virtual machine A Java virtual machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode. The JVM is detailed by a specification that formally describes ...
. Others have developed compilers, for different programming languages, to target the Java virtual machine, such as: *
ColdFusion Adobe ColdFusion is a commercial rapid web-application development computing platform created by J. J. Allaire in 1995. (The programming language used with that platform is also commonly called ColdFusion, though is more accurately known as CF ...
*
JRuby JRuby is an implementation of the Ruby programming language atop the Java Virtual Machine, written largely in Java. It is free software released under a three-way EPL/GPL/LGPL license. JRuby is tightly integrated with Java to allow the embedding ...
and
Jython Jython is an implementation of the Python programming language designed to run on the Java platform. The implementation was formerly known as JPython until 1999. Overview Jython programs can import and use any Java class. Except for some standa ...
, two
scripting language A scripting language or script language is a programming language that is used to manipulate, customize, and automate the facilities of an existing system. Scripting languages are usually interpreted at runtime rather than compiled. A scripting ...
s based on
Ruby A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sa ...
and
Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (pro ...
*
Apache Groovy Apache Groovy is a Java-syntax-compatible object-oriented programming language for the Java platform. It is both a static and dynamic language with features similar to those of Python, Ruby, and Smalltalk. It can be used as both a programming lan ...
, optionally typed and dynamic general-purpose language, with static-typing and static compilation capabilities * Scala, a type-safe general-purpose programming language supporting object-oriented and functional programming *
JGNAT A gnat () is any of many species of tiny flying insects in the dipterid suborder Nematocera, especially those in the families Mycetophilidae, Anisopodidae and Sciaridae. They can be both biting and non-biting. Most often they fly in large num ...
and AppletMagic, compile from the language
Ada Ada may refer to: Places Africa * Ada Foah, a town in Ghana * Ada (Ghana parliament constituency) * Ada, Osun, a town in Nigeria Asia * Ada, Urmia, a village in West Azerbaijan Province, Iran * Ada, Karaman, a village in Karaman Province, ...
to Java bytecode * C to Java byte-code compilers *
Clojure Clojure (, like ''closure'') is a dynamic and functional dialect of the Lisp programming language on the Java platform. Like other Lisp dialects, Clojure treats code as data and has a Lisp macro system. The current development process is comm ...
, a functional, immutable, general-purpose programming language in the
Lisp A lisp is a speech impairment in which a person misarticulates sibilants (, , , , , , , ). These misarticulations often result in unclear speech. Types * A frontal lisp occurs when the tongue is placed anterior to the target. Interdental lisping ...
family with a strong emphasis on concurrency * Kawa, an implementation of the
Scheme A scheme is a systematic plan for the implementation of a certain idea. Scheme or schemer may refer to: Arts and entertainment * ''The Scheme'' (TV series), a BBC Scotland documentary series * The Scheme (band), an English pop band * ''The Schem ...
programming language, also a dialect of
Lisp A lisp is a speech impairment in which a person misarticulates sibilants (, , , , , , , ). These misarticulations often result in unclear speech. Types * A frontal lisp occurs when the tongue is placed anterior to the target. Interdental lisping ...
. * MIDletPascal *
JavaFX Script JavaFX Script was a scripting language designed by Sun Microsystems, forming part of the JavaFX family of technologies on the Java Platform. JavaFX targeted the Rich Internet Application domain (competing with Adobe Flex and Microsoft Silverlight ...
code is compiled to Java bytecode * Kotlin, a statically-typed general-purpose programming language with type inference *
Object Pascal Object Pascal is an extension to the programming language Pascal (programming language), Pascal that provides object-oriented programming (OOP) features such as Class (computer programming), classes and Method (computer programming), methods. ...
source code is compiled to Java bytecode using the
Free Pascal Free Pascal Compiler (FPC) is a compiler for the closely related programming-language dialects Pascal and Object Pascal. It is free software released under the GNU General Public License, witexception clausesthat allow static linking against it ...
3.0+ compiler.


Execution

There are several Java virtual machines available today to execute Java bytecode, both free and commercial products. If executing bytecode in a virtual machine is undesirable, a developer can also compile Java source code or bytecode directly to native machine code with tools such as the
GNU Compiler for Java The GNU Compiler for Java (GCJ) is a free compiler for the Java programming language. It was part of the GNU Compiler Collection for over ten years but as of 2017 it is no longer maintained and will not be part of future releases. GCJ compiles ...
(GCJ). Some processors can execute Java bytecode natively. Such processors are termed ''
Java processor A Java processor is the implementation of the Java virtual machine (JVM) in hardware. In other words, the Java bytecode that makes up the instruction set of the abstract machine becomes the instruction set of a concrete machine. These were the most ...
s''.


Support for dynamic languages

The
Java virtual machine A Java virtual machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode. The JVM is detailed by a specification that formally describes ...
provides some support for dynamically typed languages. Most of the extant JVM instruction set is
statically typed In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a type to every "term" (a word, phrase, or other set of symbols). Usually the terms are various constructs of a computer progra ...
- in the sense that method calls have their signatures type-checked at
compile time In computer science, compile time (or compile-time) describes the time window during which a computer program is compiled. The term is used as an adjective to describe concepts related to the context of program compilation, as opposed to concept ...
, without a mechanism to defer this decision to
run time Run(s) or RUN may refer to: Places * Run (island), one of the Banda Islands in Indonesia * Run (stream), a stream in the Dutch province of North Brabant People * Run (rapper), Joseph Simmons, now known as "Reverend Run", from the hip-hop group ...
, or to choose the method dispatch by an alternative approach. JSR 292 (''Supporting Dynamically Typed Languages on the Java Platform'')see JSR 292
/ref> added a new invokedynamic instruction at the JVM level, to allow method invocation relying on dynamic
type checking In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a type to every "term" (a word, phrase, or other set of symbols). Usually the terms are various constructs of a computer progra ...
(instead of the extant statically type-checked invokevirtual instruction). The
Da Vinci Machine The Da Vinci Machine, also called the Multi Language Virtual Machine, was a Sun Microsystems project aiming to prototype the extension of the Java Virtual Machine (JVM) to add support for dynamic languages. It was already possible to run dynami ...
is a prototype virtual machine implementation that hosts JVM extensions aimed at supporting dynamic languages. All JVMs supporting JSE 7 also include the invokedynamic opcode.


See also

* Java bytecode instruction listings *
Java class file A Java class file is a file (with the filename extension A filename extension, file name extension or file extension is a suffix to the name of a computer file (e.g., .txt, .docx, .md). The extension indicates a characteristic of the file cont ...
*
List of JVM languages This list of JVM Languages comprises notable computer programming languages that are used to produce computer software that runs on the Java virtual machine (JVM). Some of these languages are interpreted by a Java program, and some are compiled ...
*
Java backporting tools Java backporting tools are programs (usually written in Java) that convert Java classes bytecodes from one version of the Java Platform Java is a set of computer software and specifications developed by James Gosling at Sun Microsystems, ...
* C to Java Virtual Machine compilers * JStik *
Common Intermediate Language Common Intermediate Language (CIL), formerly called Microsoft Intermediate Language (MSIL) or Intermediate Language (IL), is the intermediate language binary instruction set defined within the Common Language Infrastructure (CLI) specification. ...
(CIL), Microsoft's rival to Java bytecode *
ObjectWeb ASM The ASM library is a project of the OW2 consortium. It provides a simple API for decomposing, modifying, and recomposing binary Java classes (i.e. bytecode). The project was originally conceived and developed by Eric Bruneton. ASM is Java-centric a ...
*
Byte Code Engineering Library The Byte Code Engineering Library (BCEL) is a project sponsored by the Apache Foundation previously under their Jakarta charter to provide a simple API for decomposing, modifying, and recomposing binary Java classes (I.e. bytecode). The project wa ...


References


External links


Oracle's Java Virtual Machine Specification

Programming Languages for the Java Virtual Machine


* [https://web.archive.org/web/20090809232522/http://www.adaptj.com/main/stacktrace AdaptJ StackTrace – bytecode level debugging with a full control of the stack, the local variables, and the execution flow]
Java Class Unpacker – plugin for Total Commander, it lets open class files as compressed archives and see fields and methods as files. The bytecode can be viewed as text using F3
{{Java (Sun) Assembly languages
Bytecodes Bytecode (also called portable code or p-code) is a form of instruction set designed for efficient execution by a software interpreter. Unlike human-readable source code, bytecodes are compact numeric codes, constants, and references (normal ...
Bytecodes