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, FirstProgram.java.
- Compile it by typing "javac FirstProgram.java" in the terminal window.
- Run (or execute) it by typing "java FirstProgram" in the terminal window.
The first step creates the program; the second translates it into a language more suitable for machine execution (and puts the result in a file named MyProgram.class); the third actually runs the program.
1 2 3 4 5
public class Main { public static void main(String[] args) { System.out.println("Hello, World!"); } }
- Creating a Java Program - A program is nothing more than a sequence of characters like sentences or paragraphs. We create program by defining or typing that sequence of characters using a text editor in the same way as we do for e-mail. The code above is an example. Type that into your text editor and save it into a filed named Main.java.
- Compiling the Java Program - Java is high-level language. In other words, it is designed to be best understood by the programmer (that's you). We need the help of the compiler so that the computer can execute it. 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 file with the .java extension as input and produces a file with a .class extension. To compile your program, you need to type the command below at the terminal or command prompt:
% javac Main.java
- Executing a Java program - Once you compile your program, you can run it. To run your Hello World program, type the following at the terminal or command prompt:
% java Main
If all goes well, you should see the following response:
Hello, World
Congratulations!
Hello, World
Congratulations!
Mag-post ng isang Komento
Click to see the code!
To insert emoticon you must added at least one space before the code.