Header Ads

Breaking News
recent

Processing a Java Program

Processing a Java Program
Java has two types of programs—applications and applets. The following is an example of a Java application program:
public class MyFirstJavaProgram {
public static void main(String[] args) {
System.out.println("My first Java program.");
}
}
At this point you need not be too concerned with the details of this program. However, if you run (execute) this program, it will display the following line on the screen:

My first Java program.

Recall that a computer can understand only machine language. Therefore, in order to run this program successfully, the code must first be translated into the machine language. In this section we review the steps required to execute programs written in Java.
To process a program written in Java, you carry out the following steps, as illustrated in Figure 1-3.

1. You use a text editor, such as Notepad, to create (that is, type) a program in Java following the rules, or syntax, of the language. This program is called the source program. The program must be saved in a text file named ClassName.java, where ClassName is the name of the Java class contained in the file. For example, in the Java program 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 translate 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, typically, a Java program is a collection of classes. We will 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.
The program that automatically does this in Java is known as the loader.

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 program’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 instruction into your computer’s machine language, and then executes it.

FIGURE 1-3 Processing a Java program


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. The Java interpreter translates each bytecode instruction into a particular type of CPU machine language and then executes the instruction. Thus, in the case of the Java language, a different type of interpreter is needed for a particular type of CPU. However, interpreters are programs that are simpler than compilers. Because the Java interpreter translates one bytecode instruction at a time, Java programs run more slowly.

No comments:

Powered by Blogger.