Java Reference
In-Depth Information
Recall that the computer understands only machine language. Moreover, different types
of CPUs use different machine languages. To make Java programs machine indepen-
dent, that is, able to run on many different types of computer platforms, the designers of
Java introduced a hypothetical computer called the Java Virtual Machine (JVM).In
fact, bytecode is the machine language for the JVM.
In languages such as C and C++, the compiler translates the source code directly into the
machine language of your computer's CPU. For such languages, a different compiler is
needed for each type of CPU. Therefore, programs in these languages are not easily
portable from one type of machine to another. The source code must be recompiled for
each type of CPU. To make Java programs machine independent and easily portable, and
to allow them to run on a Web browser, the designers of Java introduced the Java Virtual
Machine (JVM) and bytecode as the (machine) language of this machine. It is easier to
translate a bytecode into a particular type of CPU. This concept is covered further in the
following section, 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
 
 
Search WWH ::




Custom Search