Here are the classic steps to develop a Java program:
Use a text editor to edit a source file SOURCEFILE.java
Compile the source file by typing
javac SOURCEFILE.java
This produces a class file SOURCEFILE.class. If there are errors during compilation, then go back to the previous step.
Run the class file by typing
java SOURCEFILE
If there are errors during interpretation like not getting the expected output, then go back to the previous step.
A source file ends in .java
. It is usually known as the “java program”. It contains your instructions written in the java programming langauge.
A class file ends in .class
. It contains bytecode
instructions, i.e., strings of 0’s and 1’s that are instructions for the Java Virtual Machine (JVM), and are equivalent to the java source program.
A compiler translates a program written in a high-level language into an equivalent machine language program.
The compiler that translates a java program into JVM machine language is named javac.
A JVM is usually implemented in software. Though rarely done, it can also be implemented in hardware.
An interpreter takes a program in the JVM language and executes it on a real processor.
The interpreter that executes JVM language program on your processor is named java.
/* Prints "Hello, World". */
public class HelloWorld {
// This is line comment
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
This program shows how to process commandline arguments.
/*************************************************************************
* Prints "Hi, Bob. How are you?" where "Bob" is replaced by the
* command-line argument.
*************************************************************************/
public class UseArgument {
public static void main(String[] args) {
System.out.print("Hi, ");
System.out.print(args[0]);
System.out.println(". How are you?");
}
}
is called a program error (or bug).
A special kind of compile-time error is called syntax error. It occurs when your java program violates some rules of how a java program should be written. Another kind of compile-time error is called type error. It occurs when a program statement violates some rules of type usage.
A special kind of run-time error is called logic error. It is caused by a mistake in the algorithm and is usually the nastiest kind of bug.
Show example compile-time errors
Show example run-time errors
An executing program can get its input in two ways.
An executing program normally writes its output to the computer screen.
Both input and output are in the form of text streams.