Java bytecode
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 JavaIn 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 architectureThe 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.6 [2] 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.[5]: 4.7.3 Each can be independently sized from 0 to 65535 values, where each value is 32 bits. Instruction setEach bytecode is composed of one byte that represents the opcode, along with zero or more bytes for operands.[5]: 2.11 Of the 256 possible byte-long opcodes, as of 2015[update], 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]: 6.2 Two of these ( 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]: 2.11.1 These are as follows:
For example, The ExampleConsider the following Java code: 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);
}
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
GenerationThe 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:
ExecutionThere 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 languagesThe 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 See also
References
External linksThe Wikibook Java Programming has a page on the topic of: Java bytecode
|
Portal di Ensiklopedia Dunia