Java bytecode explained

Java bytecode is the instruction set of the Java virtual machine (JVM), the language to which Java and other JVM-compatible source code is compiled.[1] Each instruction is represented by a single byte, hence the name bytecode, making it a compact form of data.[2]

Due to the nature of bytecode, a Java bytecode program is runnable on any machine with a compatible JVM; without the lengthy process of compiling from source code.

Java bytecode is used at runtime either interpreted by a JVM or compiled to machine code via just-in-time (JIT) compilation and run as a native application.

As Java bytecode is designed for a cross-platform compatibility and security, a Java bytecode application tends to run consistently across various hardware and software configurations.[3]

Relation to Java

In general, a Java programmer does not need to understand Java bytecode or even be aware of it. However, as suggested in the IBM developerWorks journal, "Understanding bytecode and what bytecode is likely to be generated by a Java compiler helps the Java programmer in the same way that knowledge of assembly helps the C or C++ programmer."[4]

Instruction set architecture

The bytecode comprises various instruction types, including data manipulation, control transfer, object creation and manipulation, and method invocation, all integral to Java's object-oriented programming model.[1]

The JVM is both a stack machine and a register machine. Each frame for a method call has an "operand stack" and an array of "local variables".[5] [2] 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.[5] 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[5] (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).[5]

Instruction set

Each bytecode is composed of one byte that represents the opcode, along with zero or more bytes for operands.[5]

Of the 256 possible byte-long opcodes,, 202 are in use (~79%), 51 are reserved for future use (~20%), and 3 instructions (~1%) are permanently reserved for JVM implementations to use.[5] 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:

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.[5] 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 (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 (computer programming)|this]] 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_21: istore_12: iload_13: sipush 10006: if_icmpge 449: iconst_210: istore_211: iload_212: iload_113: if_icmpge 3116: iload_117: iload_218: irem19: ifne 2522: goto 3825: iinc 2, 128: goto 1131: getstatic #84; // Field java/lang/System.out:Ljava/io/PrintStream;34: iload_135: invokevirtual #85; // Method java/io/PrintStream.println:(I)V38: iinc 1, 141: goto 244: return

Generation

The most common language targeting Java virtual machine by producing Java bytecode is Java. Originally only one compiler existed, the javac 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:

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. Notable Java assemblers include:

Others have developed compilers, for different programming languages, to target the Java virtual machine, such as:

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 (GCJ). Some processors can execute Java bytecode natively. Such processors are termed Java processors.

Support for dynamic languages

The Java virtual machine provides some support for dynamically typed languages. Most of the extant 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.[12]

JSR 292 (Supporting Dynamically Typed Languages on the Java Platform)[13] added a new invokedynamic instruction at the JVM level, to allow method invocation relying on dynamic type checking (instead of the extant statically type-checked invokevirtual instruction). The Da Vinci Machine 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

External links

Notes and References

  1. Web site: Java Virtual Machine Specification. Oracle. 14 November 2023.
  2. Book: Lindholm, Tim. The Java Virtual Machine Specification. 2015. Oracle. 978-0133905908.
  3. Arnold. Ken. The Java Programming Language. Sun Microsystems. 1996. 1. 1. 30–40.
  4. Web site: IBM Developer . 20 February 2006 . developer.ibm.com.
  5. Book: Lindholm . Tim . Yellin . Frank . Bracha . Gilad . Buckley . Alex . The Java Virtual Machine Specification . Java SE 8 . 2015-02-13 .
  6. Web site: Jasmin Home Page. jasmin.sourceforge.net. 2 June 2024.
  7. Web site: Jamaica: The Java virtual machine (JVM) macro assembler. 2 June 2024.
  8. Web site: Storyyeller/Krakatau. 1 June 2024. 2 June 2024. GitHub.
  9. Web site: Lilac - a Java assembler. lilac.sourceforge.net. 2 June 2024.
  10. Web site: FPC New Features 3.0.0 - Free Pascal wiki. wiki.freepascal.org. 2 June 2024.
  11. Web site: FPC JVM - Free Pascal wiki. wiki.freepascal.org. 2 June 2024.
  12. Web site: InvokeDynamic: Actually Useful?. 2007-01-03. Nutter. Charles. 2008-01-25.
  13. Web site: The Java Community Process(SM) Program - JSRs: Java Specification Requests - detail JSR# 292. www.jcp.org. 2 June 2024.