Content deleted Content added
Expanded and updated the lead section of the Java bytecode article to provide a more comprehensive overview. Included additional details about the nature and function of Java bytecode, its role in the JVM, and the significance of Just-In-Time (JIT) compilation for performance enhancement. |
WP Randomno (talk | contribs) |
||
(26 intermediate revisions by 22 users not shown) | |||
Line 3:
{{Use American English|date=November 2023}}
'''Java bytecode''' is the instruction set of the [[Java virtual machine]] (JVM),
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.
In the JVM, Java bytecode operates as a set of instructions for both a stack machine and a register machine, utilizing an operand stack and local variables for executing operations<ref name="JVM Book"/>. 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"/>.▼
As Java bytecode is designed for a cross-platform compatibility and security, a Java bytecode application tends to run consistently across various [[computer hardware|hardware]] and [[software]] configurations.<ref>{{Cite journal|last=Arnold|first=Ken|title=The Java Programming Language|journal=Sun Microsystems|year=1996|volume=1|issue=1|pages=30–40}}</ref>
== Relation to Java ==
== Instruction set architecture ==
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}} 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 [[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}}▼
▲
▲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 ===
{{further|List of Java bytecode instructions}}
Line 33 ⟶ 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 71 ⟶ 77:
continue outer;
}
System.out.println
}
</syntaxhighlight>
Line 111 ⟶ 117:
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>
*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>
*Krakatau Bytecode Tools, currently contains three tools: a decompiler and disassembler for Java classfiles and an assembler to create classfiles.<ref>
*Lilac, an assembler and disassembler for the [[Java virtual machine]].<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
Line 125 ⟶ 131:
*[[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]].
*
*[[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>
== Execution ==
Line 139 ⟶ 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=
| title=InvokeDynamic: Actually Useful?
| date=2007-01-03
Line 145 ⟶ 151:
| access-date=2008-01-25}}</ref>
[[Java Community Process|JSR]] 292 (''Supporting Dynamically Typed Languages on the Java Platform'')<ref>
== See also ==
{{Portal|Computer programming}}
<!---♦♦♦ Please keep the list in alphabetical order ♦♦♦--->
* [[List of Java bytecode instructions]]▼
* [[
* [[Common Intermediate Language]] (CIL), Microsoft's rival to Java bytecode▼
* [[List of JVM languages]]▼
* [[Java backporting tools]]
* [[Java class file]]
* [[Java virtual machine]]
* [[JStik]]
▲* [[Common Intermediate Language]] (CIL), Microsoft's rival to Java bytecode
* [[ObjectWeb ASM]]
▲* [[List of Java bytecode instructions]]
▲* [[List of JVM languages]]
== References ==
|