Content
1.4
The
Java Environment Basics
1.4.3
How Java programs work on the computer
There are three steps needed to create
and run a Java program:
Create a source file
Compile the source file
Run the program
First, create a source file containing
the instructions for the program in the Java language. This file must
always have .java as the suffix of the filename. The suffix of a file
name is called a file extension, and is found after the period in the
file name. Figure
shows the code for the source file named
FirstProgram.java.
For example, suppose a student is writing a program that creates and
maintains a directory of all the students in a Java class. To call the
program StudentDirectory, the file containing the source code must be
called StudentDirectory.java. There are coding conventions that many
Java developers use to write their code. In this example, the name of
the file is a compound noun, made from the words Student and Directory, and the first
letter of each word in the name of the file is capitalized. These
conventions will continue to appear as the chapter discusses more
about the Java language.
The second step is to compile the
source code into bytecode for the JVM. The Java compiler, called javac, is a program that performs this translation from source code to bytecode. The Java compiler creates a file with the same name as the
source file, but with a suffix or file extension of .class
.
Note: the filename in the
window title is FirstProgram.class.
Provided there are no compiler errors,
the third step is to start the JVM and instruct it to run the program.
The JVM is a program called java, and it will execute the program on
the computer. Figure
shows the flow of activity from creating a source file to running a
Java program. The JVM interprets the bytecode of the class file to the specific
language of the machine. The programmer has to compile the source file only
once and can run it many times on different computers.
The next section will discuss some
basic Java syntax and vocabulary. It will also outline the steps of
creating a simple Java program.
Wyszukiwarka
Podobne podstrony:
contentcontentcontentcontentcontentcontentcontentcontentcontentfunction domnode get contentcontentcontentcontentcontentcontentwięcej podobnych podstron