ArticleslgStudy

computer science

JVM bytecode

JVM bytecode is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand JVM bytecode rather than just read about it. In short: JVM bytecode is the instruction set architecture (ISA) of the Java virtual machine (JVM), the language to which Java and other JVM-compatible source code is compiled. Each instruction is represented by one byte, hence the name bytecode, making it a compact form of data.

Key takeaways

  • JVM bytecode belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect JVM bytecode to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of JVM bytecode from memory before moving on to harder problems.

Reference excerpt

JVM bytecode is the instruction set architecture (ISA) of the Java virtual machine (JVM), the language to which Java and other JVM-compatible source code is compiled. Each instruction is represented by one byte, hence the name bytecode, making it a compact form of data. Due to the nature of virtual machines and bytecode, a JVM bytecode program is runnable on any machine with a compatible JVM, without the long process of compiling from source code. JVM 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 JVM bytecode is designed for cross-platform software compatibility and security, a JVM bytecode application tends to run consistently across various hardware and software configurations.

Relation to Java In general, a Java programmer does not need to understand JVM 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."

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. 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". The operand stack is used for passing 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. long and double 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 is composed of one byte that represents the opcode, along with zero or more bytes for operands. Of the 256 possible byte-long opcodes, as of 2015, 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 null 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 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:

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

Generation

The most common language targeting Java virtual machine by producing JVM bytecode is Java. Originally only one compiler existed, the javac compiler from Sun Microsystems, which compiles Java source code to JVM bytecode; but because all the specifications for JVM bytecode are now available, other parties have supplied compilers that produce JVM bytecode. Examples of other compilers include:

Eclipse compiler for Java (ECJ) Jikes, compiles from Java to JVM bytecode (developed by IBM, implemented in C++) Espresso, compiles from Java to JVM bytecode (Java 1.0 only) GNU Compiler for Java (GCJ), compiles from Java to JVM bytecode; it can also compile to native machine code and was part of the GNU Compiler Collection (GCC) up until version 6. Some projects provide Java assemblers to enable writing JVM 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:

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 for the Java virtual machine. 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. Others have developed compilers, for different programming languages, to target the Java virtual machine, such as:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with JVM bytecode

Start with the simplest possible case. Write down what JVM bytecode claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to JVM bytecode before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about JVM bytecode ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of JVM bytecode

In research
JVM bytecode appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses JVM bytecode in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
JVM bytecode is common in secondary-school and first-year university syllabi. It links to neighbouring topics Assembly languages, Bytecodes, Java (software platform), so understanding it makes those chapters shorter.
In everyday life
Look for JVM bytecode outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “JVM bytecode” →

How to study JVM bytecode in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what JVM bytecode means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain JVM bytecode out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is JVM bytecode in simple terms?

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

Why does JVM bytecode matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study JVM bytecode?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on JVM bytecode.

Tags

  • Assembly languages
  • Bytecodes
  • Java (software platform)

Keep exploring