Java Reference
In-Depth Information
given above, the name of the ( public ) class containing the Java program
is MyFirstJavaProgram . Therefore, this program must be saved in the
text file named MyFirstJavaProgram.java . Otherwise an error will
occur.
Source program: A program written in a high-level language.
2. You must verify that the program obeys the rules of the programming
language—that is, the program must be syntactically correct—and trans-
late the program into the equivalent bytecode. The compiler checks the
source program for syntax errors and, if no error is found, translates the
program into bytecode. The bytecode is saved in the file with the .class
extension. For example, the bytecode for MyFirstJavaProgram.java
is stored in the MyFirstJavaProgram.class file by the compiler.
3. To run a Java application program, the .class file must be loaded into
computer memory. To run a Java applet, you must use either a Web
browser or an applet viewer, a stripped-down Web browser for running
applets. The programs that you write in Java are typically developed using
an integrated development environment (IDE). The IDE contains
many programs that are useful in creating your program. For example, it
contains the necessary code to display the results of the program and several
mathematical functions to make the programmer's job somewhat easier.
Because certain code is already available to you, you can use this code rather
than writing your own. You can also develop your own libraries (called
packages in Java). (Note that in Java, typically, a package is a set of related
classes.So,typica ly,aJavaprogramisaco lectionofclasses.Wew l
explain this further in Chapters 2 and 8. At this point, you need not be
too concerned with these details.) In general, to successfully run a Java
program, the bytecode for classes used in the program must be connected.
TheprogramthatautomaticallydoesthisinJavaisknownastheloader.
4. The next step is to execute the Java program. In addition to connecting
the bytecode from various classes, the loader also loads your Java pro-
gram's bytecode into main memory. As the classes are loaded into main
memory, the bytecode verifier verifies that the bytecode for the classes is
valid and does not violate Java's security restrictions. Finally, a program
called an interpreter translates each bytecode instruction into your
computer's machine language, and then executes it.
Interpreter: A program that reads and translates each bytecode instruc-
tion into your computer's machine language, and then executes it.
Note that the Java interpreter translates and executes one bytecode instruction at a time.
It does not first translate the entire bytecode into your computer's machine language. As
noted earlier, in languages such as C++, a different compiler is needed for each type
of CPU, whereas a Java compiler translates a Java source program into bytecode,
the machine language of JVM, which is independent of any particular type of CPU.
1
 
Search WWH ::




Custom Search