Java Reference
In-Depth Information
y
compiled
by
executed
by
generates
Welcome.java
(Java source-
code file)
Welcome.class
(Java bytecode
executable file)
Java
Compiler
Any
Computer
JVM
Library Code
(a)
(b)
F IGURE 1.16 (a) Java source code is translated into bytecode. (b) Java bytecode can be executed on any computer with a
Java Virtual Machine.
Figure 1.16a. The Java language is a high-level language, but Java bytecode is a low-level lan-
guage. The bytecode is similar to machine instructions but is architecture neutral and can run
on any platform that has a Java Virtual Machine (JVM) , as shown in Figure 1.16b. Rather than
a physical machine, the virtual machine is a program that interprets Java bytecode. This is one
of Java's primary advantages: Java bytecode can run on a variety of hardware platforms and
operating systems. Java source code is compiled into Java bytecode and Java bytecode is
interpreted by the JVM. Your Java code may use the code in the Java library. The JVM exe-
cutes your code along with the code in the library.
To execute a Java program is to run the program's bytecode. You can execute the bytecode
on any platform with a JVM, which is an interpreter. It translates the individual instructions in
the bytecode into the target machine language code one at a time rather than the whole pro-
gram as a single unit. Each step is executed immediately after it is translated.
The following command runs the bytecode for Listing 1.1:
bytecode
Java Virtual Machine (JVM)
interpret bytecode
run
java Welcome
Figure 1.17 shows the javac command for compiling Welcome.java . The compiler gen-
erates the Welcome.class file, and this file is executed using the java command.
javac command
java command
Note
For simplicity and consistency, all source-code and class files used in this topic are
placed under c:\book unless specified otherwise.
c:\book
Compile
Show files
VideoNote
Compile and run a Java
program
Run
F IGURE 1.17
The output of Listing 1.1 displays the message “Welcome to Java!”
 
 
Search WWH ::




Custom Search