7. Edit - compile - test
Edit - compile - test
The process of building a complete and functioning Java application begins with a text editor. The code is typed into the editor interface, and then saved as .java source files. Source files are submitted to the compiler, which adds any library files required by the code, and then produces compiled .class files. These are interpreted by the Java interpreter at run time, and the results are output as required.
This process is not always as smooth and simple as it sounds, however. It is very difficult to produce a faultless program that runs perfectly the first time it is invoked. Rather, a process of editing source files, trying to compile them, testing them once they successfully compile, and then returning to the editor to fix whatever problems may have been discovered, and then starting the whole process again, is followed. There are two kinds of problem that can cause this process to be rough - syntax errors, and logic errors. Let's examine the kinds of things that can go wrong in this process, and cause us to have to return to the editor to fix them.
Syntax errors
Syntax errors are mistakes in the way code is typed; these can be spelling errors in the names of classes and methods, punctuation in the wrong place or left out, incorrect use of brackets, incorrect use of method arguments, etc. Open your HelloWorldApp.java source file, and misspell the name of the class variable in the method call, like this:
System.oyt.println("Hello World!");
Re-save the file, and now compile it with the Java compiler by using javac HelloWorldApp.java just as before. This is what you should see:
The compiler has refused to compile the source code because we have made a syntax error by misspelling the name of the class variable in the method call. The Java compiler explains this and uses the caret ^ to point out where the error is in the code, and also tells us which line in the program code contains the problem - line 8 in this case; see HelloWorldApp.java:8 in the image above. Fix this error, and then delete the second quotation mark that delimits the string "Hello World!" - like this:
System.out.println("Hello World!);
Save and compile the file again - this is what should happen:
Once again, the syntax error - missing string delimiter - causes the compiler to refuse to compile the program. Notice that the compiler has detected two errors; there is really only one error in the file, but this error has caused the rest of the code not to make proper sense any more. The compiler tries to make sense of the rest of the code, but can't - so it sends the second error message along with the first. Sometimes a single syntax error will cause the compiler to send a large number of error messages to the console. The best thing to do when dealing with errors is to fix the first one in the list, recompile, see how many error messages have disappeared, fix the first one in the remaining list, and so on until the code compiles without any errors.
Logic errors
Now fix the missing quote error, and then change the string inside the quotes to read:
System.out.println("Hell World!");
and then save and recompile the file. This time, the code compiles with no problems, but if you now run it you will see
Hell World!
appear on the screen. The program has compiled and run, but the result is incorrect nonetheless. This is a run-time or logic error, and errors of this type are not trapped by the compiler because it cannot make any decisions about the logic that the programmer is following in her code. We still have to go back to the editor and fix this error nonetheless.
This process of writing, compiling, running and fixing code is known as the edit - compile - test cycle, and in a large program it will need to be followed many times before the code is completely ready to be run. This cycle is a normal part of the programming experience, and you should be prepared to follow it for as many times as it takes to get your code fully debugged. Careful attention to detail as you generate your code will help you to minimise the number of repetitions of this cycle, but be prepared to loop through it a few times! Java makes this process much easier by having an excellent compiler that traps all syntax errors, and points out exactly what and where they are into the bargain. This means that when it comes time to run your code for the first time, you can be almost certain that any errors that occur are logic errors - the number of things that can go wrong has been cut in half by the compiling process.






