next up previous contents
Next: Comments Up: 1 Programming Basics Previous: Basic program structure   Contents

Subsections

Running a program on a lab PC

In this section you will learn how to run your first Java program.

Here is perhaps the simplest program you can write in Java.

/**
 *  Purpose: To write the word `Hello' to the screen.
 */

public class Hello
{
    public static void main(String[] argv)
    {
        System.out.println("Hello");      // outputs Hello to the screen
    }
}

The program simply writes `Hello' to your screen. You will see that it has the same basic structure as was shown before. The semi-colon, ;, is used in Java to indicate the end of a line of code, or one instruction. Don't worry about understanding the System.out.println just yet, the program is just for use in the exercise that follows. The purpose of this exercise is to show you how to run Java programs on the computers you are using. You will now be taken through typing in the program and running it on the lab PCs.

Exercise 1.1


(1 mark)



You will be shown in the lab session how to use your computer to write, compile and run Java programs.

Following the demonstration, type in the example above, save it, compile it and run it. Read through the advice below as you work.



In appendix A you will find some notes on how to use Java on your PC.

Writing

Type in the program exactly as shown in the example above. Take care to copy upper and lower case correctly from the example, as Java is case sensitive: the program will not work if you get this wrong.

Saving

The code should always be saved in a file that has the same name as the public class and with the extension .java so you should save the above code under the name `Hello.java'. This is your source file. Note that the filename is case sensistive.

Compilation

The compilation process produces a file called Hello.class, which then is used when you run the program.

If you get error messages when you compile the program, check very carefully what you have typed, then try again or ask a demonstrator. Beware that there is a distinction made between upper and lower case letters, so if for example you type Class rather than class, you will get error messages.

Especially when you are creating longer programs you are bound to make mistakes so you will get often errors when you first compile a program. Deal with one error at a time starting from the first that the compiler found. Sometimes an error at the beginning of a program can cause many errors later on and if you correct the first problem and compile again you may find some of the other errors have vanished. The error message will contain the line number and some description of the error which you can use to help find what went wrong. Appendix B contains more help on errors, you should refer to this when you come across problems.

Running

You should now see the word `Hello' printed on the screen.

If you don't then you may get a message about an exception written to your screen though it is unlikely to happen with a program as small as we are dealing with here. Exceptions arise from problems that occur during the running of the program that were not detected by the compiler. You will be warned about the most common exceptions as we go along.

If you have an exception now, ask for help. You will learn how to solve these problems yourself as you gain experience.


next up previous contents
Next: Comments Up: 1 Programming Basics Previous: Basic program structure   Contents
RHUL Dept. of Physics