Recognizing Syntax Errors

When you make syntax errors in your program the compiler gives error messages and does not create the bytecode file. It saves time and frustration to learn what some of these messages are and what they mean. Unfortunately, at this stage in the game many of the messages will not be meaningful except to let you know where the first error occurred. Your only choice is to carefully study your program to find the error. In the following you will introduce a few typical errors into a simple program and examine the error messages.

  1. Type the following program into a file called Hello.java. (This is the traditional first program a computer scientist writes in any new language.)
    // ********************************************
    //   Hello.java
    // 
    //   Print a Hello, World message.
    // ********************************************
    
    public class Hello
    {
        // -----------------------------------
        // main method -- prints the greeting
        // -----------------------------------
        public static void main (String[] args)
        {
    	    System.out.println ("Welcome, world!");
        }
    }
    
    

    Compile and run the program to see what it does. (It should run! If it doesn't, you've copied the text incorrectly into Hello.java.) Then make the changes below, providing your answers to the questions in the "Recognizing Errors" section of your lab document. In that section, please number your answers, "2", "3", etc.

  2. Undeclared identifier. Delete one l (el) from the "println", save the program, and recompile it. What was the error message?

  3. Misspelling inside string. Correct the mistake above, then delete one l from the "Welcome" in the message to be printed (inside the quotation marks). Save the program and recompile it. There is no error message -- why not? Now run the program. What has changed?

  4. No ending quotation mark in a string literal. Correct the spelling in the string, then delete the ending quotation mark (") enclosing the string Hello, World!. Save the program and recompile it. What error message(s) do you get? Why do you think the compiler thinks that is the problem, rather than saying something like, "missing quotation mark"?

  5. No beginning quotation mark in a string literal. Put the ending quotation mark back, then take out the beginning one. Save and recompile. What is the error message? Why do you think the compiler thinks that is the problem, rather than saying something like, "missing quotation mark"?

  6. No semicolon after a statement. Fix the last error (put the quotation mark back). Now remove the semicolon at the end of the line that prints the message. Save the program and recompile it. What error message(s) do you get?

To hand in:

Your lab document, answering each of the queries above, #2-6.