Java Basics

The Java programming language is a general-purpose object-oriented concurrent language. Its syntax is similar to C and C++, but it omits many of the features that make C and C++ complex, confusing, and unsafe.

Java programs run on a standardized platform called the Java virtual machine. The Java virtual machine is an abstract computing machine. Like a real computing machine, it has an instruction set and manipulates various memory areas at run time.

In this class, we will be using the Java 2 Platform, Standard Edition v1.4.0. This is installed on the "elaine" machines in Sweet Hall, and also is available for download at http://java.sun.com/j2se/downloads.html.

This page assumes that you've got some basic familiarity with the Java language, but haven't had to use it on the elaines. If you've never dealt with Java before, read The Java Tutorial and familiarize yourself with the language. (The first part, Your first cup of Java, covers the basics of installing and running Java on your platform of choice.)

Using the Java Development Kit

There are three main programs that you will use in the JDK:

All of these run from the command line. We will delay discussion of javap until our discussion of the Java bytecode system, and concentrate more on the more frequently used javac and java.

Create the following file on an elaine machine, and name it Hello.java:

public class Hello {
  public static void main(String[] args) {
    System.out.println("Hello, world!");
  }
}

Now execute the following commands from that directory. You should get the same results as listed here.

elaine6:~/examples> javac Hello.java
elaine6:~/examples> java Hello
Hello, world!
elaine6:~/examples>

The javac command reads Hello.java and produces Hello.class, which is then executed by the java command. Note that you do not specify the .class extension when running code.

If your shell doesn't find these programs, you'll have to extend your PATH environment variable. The command

setenv PATH ${PATH}:/usr/pubsw/bin

should fix the problem.

You may also want to use jikes instead of javac to compile you files. To do that, you should add /usr/class/cs243/bin to you PATH.

The CLASSPATH

One of Java's more unusual features is that there is no static linking step. The name of any class provides sufficient information to allow the java command to locate the relevant class file. Package names correspond to directories, and the name of the .class file must correspond to the name of the class given. The VM, however, must be informed where to begin searching. This is the purpose of the CLASSPATH variable. This is a list of directories, much like the PATH variable under Unix or Win32. Each directory listed is a possible "root" for the class search. Archives of Java code (which usually have the extension .jar) may also be listed as if they were directories.

With the 1.4 JDK, CLASSPATH must include the current directory, named by a single period. (If CLASSPATH is completely undefined, that's OK too.)

When doing programming projects for CS243, you will need to set your CLASSPATH so that it points at some copy of the joeq system. More on that in the next section.

home next -->