Java bytecode: Difference between revisions

Content deleted Content added
nothing
Tags: Reverted Visual edit Mobile edit Mobile web edit
 
(15 intermediate revisions by 13 users not shown)
Line 5:
'''Java bytecode''' is the instruction set of the [[Java virtual machine]] (JVM), the language to which [[Java (programming language)|Java]] and other JVM-compatible [[source code]] is [[compiler|compiled]].<ref name="oracle jvm spec">{{Cite web|url=http://docs.oracle.com/javase/specs/jvms/se8/html/|title=Java Virtual Machine Specification|publisher=Oracle|access-date=14 November 2023}}</ref> Each instruction is represented by a single [[byte]], hence the name [[bytecode]], making it a compact form of [[data]].<ref name="JVM Book">{{Cite book|last=Lindholm|first=Tim|title=The Java Virtual Machine Specification|year=2015|publisher=Oracle|isbn=978-0133905908}}</ref>
 
Due to the nature of bytecode, a Java bytecode [[computer program|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 (program lifecycle phase)|runtime]] either [[interpreter (computing)|interpreted]] by a JVM or compiled to machine code via [[Just-in-time compilation|just-in-time]] (JIT) compilation and run as a native application.
Line 18:
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.<ref name="oracle jvm spec"/>
 
The JVM is both a [[stack machine]] and a [[register machine]]. Each [[Call stack#STACK-FRAME|frame]] for a method call has an "operand stack" and an array of "local variables".<ref name="jvm">{{cite book |last1=Lindholm |first1=Tim |last2=Yellin |first2=Frank |last3=Bracha |first3=Gilad |last4=Buckley |first4=Alex |title=The Java Virtual Machine Specification |edition=Java SE 8 |date=2015-02-13 |url=http://docs.oracle.com/javase/specs/jvms/se8/html/}}</ref>{{rp|2.6}} <ref name="JVM Book"/> 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 [[Processor register|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.<ref name="jvm"/>{{rp|4.7.3}} Each can be independently sized from 0 to 65535 values, where each value is 32 bits. {{code|lang="java"|long}} and {{code|lang="java"|double}} types, which are 64 bits, take up two consecutive local variables<ref name="jvm"/>{{rp|2.6.1}} (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).<ref name="jvm"/>{{rp|2.6.2}}
 
=== Instruction set ===
Line 39:
There are also a few instructions for a number of more specialized tasks such as exception throwing, synchronization, etc.
 
Many instructions have [[Opcode prefix|prefixes]] and/or suffixes referring to the types of operands they operate on.<ref name="jvm"/>{{rp|2.11.1}} These are as follows:
 
{| class="wikitable"
Line 66:
The <code>const</code> instructions push a value of the specified type onto the stack. For example, <code>iconst_5</code> will push an integer (32 bit value) with the value 5 onto the stack, while <code>dconst_1</code> will push a double (64 bit floating point value) with the value 1 onto the stack. There is also an <code>aconst_null</code>, which pushes a {{code|lang=java|null}} reference. The ''n'' for the <code>load</code> and <code>store</code> instructions specifies the index in the local variable array to load from or store to. The <code>aload_0</code> instruction pushes the object in local variable 0 onto the stack (this is usually the <code>[[this (computer programming)|this]]</code> object). <code>istore_1</code> 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 ==
edits ok!
 
Consider the following Java code:
*
*
*
 
<syntaxhighlight lang="java">
*
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);
}
</syntaxhighlight>
 
A Java compiler might translate the Java code above into bytecode as follows, assuming the above was put in a method:
*[[ColdFusion|ion]]
<syntaxhighlight lang="jasmin">
*
0: iconst_2
*
1: istore_1
*programming language with type inference
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</syntaxhighlight>
 
== Generation ==
{{Further|List of JVM languages}}
 
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:
*Eclipse compiler for Java (ECJ)
*[[Jikes]], compiles from Java to Java bytecode (developed by [[IBM]], implemented in [[C++]])
*Espresso, compiles from Java to Java bytecode (Java 1.0 only)
*[[GNU Compiler for Java]] (GCJ), compiles from Java to Java 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 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:
*[[Jasmin (Java assembler)|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<ref>{{Cite web|url=https://jasmin.sourceforge.net/|title=Jasmin Home Page|website=jasmin.sourceforge.net|accessdate=2 June 2024}}</ref>
*Jamaica, a [[Macro (computer science)|macro]] [[assembly language]] for the [[Java virtual machine]]. Java syntax is used for class or interface definition. Method bodies are specified using bytecode instructions.<ref>{{Cite web|url=https://www.javaworld.com/article/2072355/core-java/learn-to-speak-jamaican.html|title=Jamaica: The Java virtual machine (JVM) macro assembler<!-- Bot generated title -->|archive-url=https://web.archive.org/web/20231114000632/https://www.infoworld.com/article/2072355/learn-to-speak-jamaican.html|archive-date=14 November 2023|work=JavaWorld |accessdate=2 June 2024 |last1=Huang |first1=James Jianbo }}</ref>
*Krakatau Bytecode Tools, currently contains three tools: a decompiler and disassembler for Java classfiles and an assembler to create classfiles.<ref>{{Cite web|url=https://github.com/Storyyeller/Krakatau|title=Storyyeller/Krakatau|date=1 June 2024|accessdate=2 June 2024|via=GitHub}}</ref>
*Lilac, an assembler and disassembler for the [[Java virtual machine]].<ref>{{Cite web|url=https://lilac.sourceforge.net/|title=Lilac - a Java assembler|website=lilac.sourceforge.net|accessdate=2 June 2024}}</ref>
 
Others have developed compilers, for different programming languages, to target the Java virtual machine, such as:
*[[ColdFusion Markup Language|ColdFusion]]
*[[JRuby]] and [[Jython]], two [[scripting language]]s based on [[Ruby (programming language)|Ruby]] and [[Python (programming language)|Python]]
*[[Groovy (programming language)|Apache Groovy]], optionally typed and dynamic general-purpose language, with static-typing and static compilation capabilities
*[[Scala (programming language)|Scala]], a type-safe general-purpose programming language supporting object-oriented and functional programming
*[[JGNAT]] and AppletMagic, compile from the language [[Ada (programming language)|Ada]] to Java bytecode
*[[Java virtual machine#C to bytecode compilers|C to Java byte-code compiler]]s {{dead link|date=December 2018}}
*[[Clojure]], a functional, immutable, general-purpose programming language in the [[Lisp (programming language)|Lisp]] family with a strong emphasis on concurrency
*[[Kawa (Scheme implementation)|Kawa]], an implementation of the [[Scheme (programming language)|Scheme]] programming language, also a dialect of [[Lisp (programming language)|Lisp]].
*MIDletPascal
*[[JavaFX Script]] code is compiled to Java bytecode
*[[Kotlin (programming language)|Kotlin]], a statically-typed general-purpose programming language with type inference
*[[Object Pascal]] source code is compiled to Java bytecode using the [[Free Pascal]] 3.0+ compiler.<ref>{{Cite web|url=https://wiki.freepascal.org/FPC_New_Features_3.0|title=FPC New Features 3.0.0 - Free Pascal wiki|website=wiki.freepascal.org|accessdate=2 June 2024}}</ref><ref>{{Cite web|url=https://wiki.freepascal.org/FPC_JVM|title=FPC JVM - Free Pascal wiki|website=wiki.freepascal.org|accessdate=2 June 2024}}</ref>
 
== Execution ==
Line 89 ⟶ 145:
 
The [[Java virtual machine]] provides some support for [[Type system#Dynamic typing|dynamically typed languages]]. Most of the extant JVM instruction set is [[Type system#Static typing|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 (program lifecycle phase)|run time]], or to choose the method dispatch by an alternative approach.<ref>{{cite web
| url=httphttps://headius.blogspot.com/2007/01/invokedynamic-actually-useful.html
| title=InvokeDynamic: Actually Useful?
| date=2007-01-03