Java Reference
In-Depth Information
Compiler creates bytecodes
and stores them on disk in a
file whose name ends
with
.class
Phase 2: Compile
Compiler
Disk
Fig. 1.7
|
Typical Java development environment—compilation phase.
The Java compiler translates Java source code into
bytecodes
that represent the tasks
to execute in the execution phase (Phase 5). The
Java Virtual Machine
(
JVM
)—a part of
the JDK and the foundation of the Java platform—executes bytecodes. A
virtual machine
(
VM
) is a software application that simulates a computer but hides the underlying oper-
ating system and hardware from the programs that interact with it. If the same VM is
implemented on many computer platforms, applications written for that type of VM can
be used on all those platforms. The JVM is one of the most widely used virtual machines.
Microsoft's .NET uses a similar virtual-machine architecture.
Unlike machine-language instructions, which are
platform dependent
(that is, depen-
dent on specific computer hardware), bytecode instructions are
platform independent
. So,
Java's bytecodes are
portable
—without recompiling the source code, the same bytecode
instructions can execute on any platform containing a JVM that understands the version
of Java in which the bytecodes were compiled. The JVM is invoked by the
java
command.
For example, to execute a Java application called
Welcome
, you'd type the command
java Welcome
in a command window to invoke the JVM, which would then initiate the steps necessary
to execute the application. This begins Phase 3. IDEs typically provide a menu item, such
as
Run
, that invokes the
java
command for you.
Phase 3: Loading a Program into Memory
In Phase 3, the JVM places the program in memory to execute it—this is known as
loading
(Fig. 1.8)
.
The JVM's
class loader
takes the
.class
files containing the program's byte-
codes and transfers them to primary memory. It also loads any of the
.class
files provided
by Java that your program uses. The
.class
files can be loaded from a disk on your system
or over a network (e.g., your local college or company network, or the Internet).
Primary
Memory
Class loader reads
.class
files
containing bytecodes
from disk and puts
those bytecodes in
memory
Phase 3: Load
Class Loader
Disk
Fig. 1.8
|
Typical Java development environment—loading phase.