1. Write your Java source code program in a text editor and save it with a .java extension. Make sure that your text editor saves the file in plain ASCII format, and make sure that it supports long file names. You can't save a Java program as a .jav file -- the extension has to be .java.
2. Compile your program from a command-line prompt, using the javac compiler that comes with the SDK. For example, for a source code file named Sample.java, you would type javac Sample.java. If all goes well, a Java class file will be produced. In our example, this file would be
called Sample.class. Remember to always specify the .java extension when compiling a Java program.
3. Run your program from a command-line prompt, using the java interpreter that comes with the SDK. For example, to run the Sample program from the previous step, you would type java Sample. To specify command-line arguments to a Java program, simply type them
after the program name, separated by spaces. Remember to never specify the .class extension when running a Java program.
4. Errors can occur when compiling or running a Java program. As you know, run-time errors are more difficult to debug than compile-time errors. When you are new to a language, however, compile-time error messages can seem very cryptic. Correcting compile-time errors can be very instructive, but if you can't get any of the examples in this tutorial to work。
public class Factorial {
public static void main(String[] args) {
if(args.length != 0) {
int num = Integer.parseInt(args[0]);
System.out.println(factorial(num));
}
}
private static int factorial(int fact) {
int result = fact;
if (fact == 0)
return result;
else {
while (fact != 1)
result *= --fact;
}
return result;
}
}