Java Language Basics

Compiling and Running a Java Program

A java program is composed of many different classes. Each of those classes must be compiled from a source file (with a .java extension) to a class file (with a .class extension). One of these classes must have the "main" function. In order to run your program, you must execute the java runtime ('java.exe' on windows; just 'java' on unix) with the name of the main class as an argument. For example, let's say you have a program consisting of just one class. The process would be as follows:

C:\MyJavaCode>javac HelloWorld.java
C:\MyJavaCode>java HelloWorld

You can pass additional command line arguments to your program following the name of the main class, e.g.

C:\MyJavaCode>java HelloWorld Vlad

This will pass 'Vlad' to the main function in the HelloWorld class.

Question: I mentioned earlier that a Java program usually consists of multiple classes, each in a separate source file. How does an application find these additional classes, since only the class with the main function is passed to the Java runtime? I will discuss this later, but the basic idea is that there is an environment variable called CLASSPATH, which is used to search for any matching ".class" files.

Language Basics

So far in the course we've talked about some of the basic elements of a Java program:
We also have discussed various programming elements commonly used in Java. You can find good explanations and examples of these concepts online. See Language Basics on Sun's Web site: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/index.html. The links below are included in this section.
Note: When discussing loops, I did not cover labels, break, and continue statements. I will briefly review these concepts in the next class. See:
The 'do...while' loop and the 'switch' statement are used much less commonly than the above constructs, but feel free to read about them:
In addition to loops and conditionals, we've talked about: