Java Reference
In-Depth Information
bytecode
Given the proliferation of hardware platforms and/or machine architectures available in today's net-
worked environment, the Java developers aimed at coming up with a cross-platform solution that
would not require developing expensive compilers to compile Java source code to machine code for
every possible target platform. In order to accomplish this goal, Java introduced a hybrid approach
to run programs by combining both compiler and interpreter technology. First, every Java source
program (BMI.java) is compiled into an intermediate language called bytecode (BMI.class), which is
platform independent. During this compilation step, errors in the code can be reported. Java bytecode
is not native machine code, so it cannot be run as such on a host computer. Instead, the bytecode will
be parsed by a platform-specific interpreter in order to run it on a particular architecture, such as
on Windows, Linux, Mac OS, Sun Solaris, and so on. Interpreters have been developed for various
platforms. All of them are implementations of the Java virtual machine (JVM). The bytecode can then
be considered as machine code for the JVM. The JVM is basically a virtual CPU complete with its
own operation codes. Irrespective of which platform you are on, the bytecode is the exact same. The
JVM is then a separate process that runs on top of a native processor. Figure 2-1 illustrates the byte-
code for a Windows 8 platform generated for a Java program based on the BMI calculator example.
static double BMI;
public BMIcalculator();
Code:
0: aload_0
1: invokespecial #12 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: ldc2_w #20 // double 60.0d
3: putstatic #22 // Field weight:D
6: ldc2_w #24 // double 1.7d
9: putstatic #26 // Field height:D
12: invokestatic #28 // Method calculateBMI:()V
15: getstatic #31 // Field java/lang/System.out:Ljava/io/PrintStream;
18: new #37 // class java/lang/StringBuilder
21: dup
22: ldc #39 // String Your BMI is
24: invokespecial #41 // Method java/lang/
// StringBuilder.
// "<init>":(Ljava/lang/String;)V
27: getstatic #44 // Field BMI:D
30: invokevirtual #46 // Method java/lang/StringBuilder.append:
(D)Ljava/lang/StringBuilder;
33: ldc #50 // String .
35: invokevirtual #52 // Method java/lang/StringBuilder.append:
(Ljava/lang/String;)Ljava/lang/StringBuilder;
38: invokevirtual #55 // Method java/lang/StringBuilder.toString:
()Ljava/lang/String;
41: invokevirtual #59 // Method java/io/PrintStream.println:
(Ljava/lang/String;)V
44: return
 
Search WWH ::




Custom Search