We break the process of programming in Java into three steps:
- Create the program by typing it into a text editor and saving it to a file named, say, MyProgram.java.
- Compile it by typing "javac MyProgram.java" in the terminal window.
- Run (or execute) it by typing "java MyProgram" in the terminal window.
Creating a Java program. A program is nothing more than a sequence of characters, like a sentence, a paragraph, or a poem. To create one, we need only define that sequence characters using a text editor in the same way as we do for e-mail. HelloWorld.java is an example program. Type these character into your text editor and save it into a file named HelloWorld.java.
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } }
Compiling a Java program. At first, it might seem to you as though the Java programming language is designed to be best understood by the computer. Actually, to the contrary, the language is designed to be best understood by the programmer (that's you). A compiler is an application that translates programs from the Java language to a language more suitable for executing on the computer. It takes a text file with the .java extension as input (your program) and produces a file with a .class extension (the computer-language version). To compile HelloWorld.java type the boldfaced text below at the terminal. (We use the % symbol to denote the command prompt, but it may appear different depending on your system.) %javac HelloWorld.javaIf you typed in the program correctly, you should see no error messages. Otherwise, go back and make sure you typed in the program exactly as it appears above. Executing a Java program. Once you compile your program, you can run it. This is the exciting part, where the computer follows your instructions. To run the HelloWorld program, type the following at the terminal:If all goes well, you should see the following response
%java HelloWorldHello, World
Tags:
java