THE UNIVERSITY OF WESTERN AUSTRALIA
Department of Computer Science

230.304 CONCURRENT PROGRAMMING

Basics of Java Language

The purpose of this document is to:

I will assume that you have some background in programming in Java as well as setting up the environment for successful execution of Java programs. If you have problems, please see your "Programming Structures" assignments and lab sheets again.

Compiling and Executing Java Programs

Your source file must have the extension .java. Invoke the compiler using the command:

where filename is the name of the file containing your Java source code. This compilation command generates new files in your current working directory with the extension class. A class file is generated for each class defined in your source file and is given the same name as the name given to the class in the source code. It is good programming practice to put the source code for each class into a separate source (ie java) file which is given the same name as the class. Strictly speaking, there can only be one public class defined per java source file and the file must have the name of the public class defined in it (this is case-sensitive); there is no restriction, however, on the number of private classes defined in the file or on class definitions where the modifier is omitted (so that the visibility is within the package). Each source file is separately compiled. Note that you will need to compile the source files of all classes required by each program.

You can execute the program using the command:

for the class file that contains the main method. This command invokes the Java interpreter. Note that the extension class must not be used in this command.

Basics of the Java language

The following programs are intended to expose you to the basic syntax of Java. Each of the programs succinctly expresses the essence of a particular generic aspect of Java's syntax. In the following, the links are to versions of the Java source code which have been annotated with explanatory comments, where appropriate. If you wish to look at the Java source code files themselves, the names are provided alongside the links below. You may wish to copy some of them to your working directory.

But firstly, here are some general features of Java programs and their structure: