Module 1: Introduction to Java

Module 1: Introduction to Java

There are four general kinds of errors a Java program can have:

  1. compiler errors
  2. run-time errors
  3. logical errors
  4. specification errors

We illustrate compiler errors in the two figures below. Figure 1.VII-1 shows a program with two errors and a single error message.

Figure 1.VII-2 shows the same program with the first error that had been reported by the compiler corrected, leaving another error that the compiler sees only after the first error has been fixed. The Java compiler will flag many other kinds of errors. The normal compiler error syntax is:

 :  :   <^ arrow="" pointing="" to="" place="" where="" the="" compiler="" detects="" a="" problem="">

In figure 1.VII-1, we have added line numbers to the original source code. Note that the error message points out the error on line 10—a semicolon that is actually missing on line 9.

Figure 1.VII-1 Anatomy of Java Compiler Error Messages

1. // File: UMUC_ErrorDemoA.java
2. // Author: instructorX
3. // Date: dd-mm-yyyy
4. // Purpose: Demonstrate compiler errors
5.
6. public class UMUC_ErrorDemoA {
7.    public static void main (String[] args) {
8.       int a = 3;
9. a = c
10. System.out.println ("a: " + a);
11.   } // end method main
12. } // end class UMUC_ErrorDemoA
Z:\>javac UMUC_ErrorDemoA.java
UMUC_ErrorDemoA.java:10: ';' expected
     System.out.println ("a: " + a);
     ^
1 error

In figure 1.VII-2 below, we have fixed the error flagged in figure 1.VII-1 by adding a semicolon, but now the compiler has found another error that was not reported earlier, this time on line 9. You should not be discouraged when this happens to you!

Figure 1.VII-2 Code with One Error Fixed and Another Revealed

1. // File: UMUC_ErrorDemoB.java
2. // Author: instructorX
3. // Date: dd-mm-yyyy
4. // Purpose: Demonstrate compiler errors
5.
6. public class UMUC_ErrorDemoB {
7.    public static void main (String[] args) {
8.       int a = 3;
9.       a = c;
10.      System.out.println ("a: " + a);
11.   } // end method main
12.} // end class UMUC_ErrorDemoB
Z:\>javac UMUC_ErrorDemoB.java
UMUC_ErrorDemoB.java:9: cannot resolve symbol
symbol : variable C
location: class M_1_610
      a = c;
          ^
1 error

The remaining error can be corrected by adding the following statement after line 8:

int c = 10 ;

 


Comments are closed.