Describe the process of writing and running a Java program r
Describe the process of writing and running a Java program, referringto the terms \"run-time\" and \"compile-time\"
Solution
1. Preparation of the program text:
To prepare the program text we have to write a file containing the program. For a Java program, the name of the file has to be ClassName.java where ClassName is the name of the class defined in the program.
E.g., First.java
The program can be written with any program that allows one to write a text file (editor).
E.g., NotePad, Emacs, ...
2. Compilation of the program
The compilation of the program is necessary to translate the program into a sequence of commands that can be directly executed by the computer. The standard Java compiler, which is part of the Java Standard Development Kit (Java SDK), is javac. To use it, you have to execute the command:
javac ClassName.java
The compilation produces as a result a file called ClassName.class, which contains the command that can be directly executed by the computer. For example:
javac First.java
creates the file First.class.
3. Execution of the compiled program
A program can be executed only after it has been compiled, i.e., when we have the file ClassName.class.
In Java the execution of a program is done through the command
java ClassName
(without .class). For example, the command
java First
causes the execution of the program First (or, more precisely, of the main method of the class First), and hence prints on the screen:
