notes


Introduction

Basics

We'll be working on computers running Sun's Solaris operating system, one of the variants of the UNIX operating system. If you're used to Microsoft Windows or Apple's MacOS, you'll find UNIX to be quite different. Windows and MacOS have a graphical user interface tightly integrated with the operating system from which all activity, from copying files to running programs, can be performed, while UNIX itself doesn't have any graphical user interface. Graphics are usually handled by a seperate system, called X Windows, which sits on top of UNIX, as well as a desktop environment for handling windowing and other desktop acitivities.

In order to handle files or start programs on UNIX machines, you generally enter commands on a command line, otherwise known as a shell. The old MSDOS command prompt was a very simple shell. The UNIX shell is a lot more sophisticated and has a much richer command set. See the "shell commands you will need" section below for a small list of shell commands you will need to know.

Programs are generally created by writing one or more text files, containing code following the syntax and semantics defined by the particular programming language being used. In interpreted languages, such as Perl or any of the varieties of Basic, the text file is passed to an interpreter, which then runs it. In compiled language, such as C or C++, the text files are passed to a compiler, which generates an executable, a program that can later be run alone.

The executable created by a standard compiler can be run only on the particular combination of processor and operating system that it was compiled for. A different executable needs to be compiled for each combination. The Java compiler, on the other hand, creates Java byte code, which can't be run directly by any real processor. Instead, it is run by the Java Virtual Machine, a piece of software that takes the generic instructions in the Java byte code and translates them into the instructions of the processor it is running on. The advantage of this is that you only need to compile your code once and can then distribute it to be run by anyone. The disadvantage is that having execution pass through an extra translation step slows things down a bit.

To create a text file, you use a text editor. A text editor is like a word processor, except it doesn't apply formatting like boldness, fonts, colors, etc. It just displays the text exactly as it is in the file. The two dominant text editors in UNIX are Emacs and vi. Everyone who uses UNIX usually has a definite preference, and people can get into heated debates about which is better. I use Emacs myself, so that's what I can teach you, but if you already know vi, use it.


Objects and Classes

Java is an object oriented programming language. What's more, Java is an unavoidably object-oriented programming language. All programs, even the simplest "hello world" program, must be built out of one or more objects.

In the real world, a system is generally built out of multiple objects, each of which fulfill a certain well-defined function. A car has steering, exhaust and an engine. A factory has owners, managers, workers, etc. Likewise, Java programs are composed of multiple objects engaging in well-defined activities. Depending on the particular case, it may be convenient to think of them as componenents, or as actors or agents in the system.

We we define a class of objects, we define two things about every such object created: the object's structure, and its behavior. By defining the structure of objects in a given class, we define what sub-objects or variables every object is composed of. These sub-components are sometimes called members or object data. When we define the behavior of objects in a given class, we define a set of actions that an object can perform. These actions are called functions or procedures or methods.

What makes the story of objects and classes a little more confusing is that members and methods can be defined for the class as a whole. A member defined for the class exists as one piece of data for the whole class, not as a seperate piece for each object created that is of the class. A method defined for the whole class can execute even if no objects of the class exist. Such a method is declared as a static method.

If you're new to programming, or have programmed, but never programmed in an object-oriented language, this might be a bit confusing. Just hang on and it'll make sense after you see some examples.


Hello World

The first program one usually learns in a programming class is a "hello world" program, which does nothing other than print a single message, usually "hello world". Although you could do it in a little less code than this, I think this is the best way for us to start:

public class HelloBot
{
    public void sayHello() { System.out.println("Hello World!"); }

    public static void main(String[] args) 
    {
        HelloBot bot = new HelloBot();
        bot.sayHello();
    }
}

According to the above class definition, HelloBots contain no data and have only one action they can perform, the sayHello method, which just prints out "Hello World!". Also, the HelloWorld class has one method called main. The main method is a special method that is the starting point, or entry point, for a program. You start a program in java by running the main method of some class. Our main method here creates one HelloBot called bot and tells it to perform its sayHello action, causing it to print "Hello World!" out to the shell, after which the program terminates.

To "write" this program, you enter the above code into a file called HelloBot.java. To do this using emacs, enter "emacs HelloBot.java &" into a shell. It is important that you capitalize exactly. The filename, up to the .java extension, must exactly match the class defined in the file, so the H and B in HelloBot.java need to be capitalized. After emacs opens, enter the code, and hit CTRL-X CTRL-S to save, or use the menus.

Now, from a shell, in the same directory as the file, type "javac HelloBot.java" and press return. This will take the source file and run it through the java compiler to produce java byte code. When it is done, you should get back your prompt. If you typed anything wrong, you'll see compilation errors. Otherwise, you should have a file named "HelloBot.class" in the directory you created the source file in. This is the file that contains the Java btyecode for the class, ready to run on a Java Virtual Machine. To run it, type "java HelloBot". You should see the message "Hello World!".


Shell commands you will need

Here are some UNIX shell commands you will need for dealing with files and directories, editing files, viewing files, compiling and running java code, etc. If you end a command with an ampersand (&), the command will run in the background and your shell will return to you immediately. Use this for opening applications such as emacs or netscape that run seperately in their own window. Otherwise, the shell will wait for the application to return and will thus be unresponsive until you close the application.

Note that when specifying directories, directories beginning with a / begin from the root directory and those not beginning with a slash are relative to the current directory. You'll mostly be specifying relative directories. The parent of the current directory is specified as two periods like "..".

cd DIRECTORY
change directory to DIRECTORY. If DIRECTORY is omitted, the command takes you to your home directory. (~/)
ls DIRECTORY
list the contents of DIRECTORY. If DIRECTORY is omitted, list contents of current directory.
mkdir DIRECTORY
make directory DIRECTORY
rmdir DIRECTORY
remove directory DIRECTORY
rm FILE
remove FILE. It is important to note that after this, FILE will really be gone and there will be no way of getting it back.
more FILE
print the contents of FILE to the terminal
emacs FILE &
edit FILE in emacs. If it does not exist, emacs will create it. You'll usually want to put an ampersand (&) at the end of the command ("emacs FILE &") so that the terminal you are typing from stays responsive.
javac JAVAFILE
compile JAVAFILE (which should end in .java) into bytecode stored in .class files. You have to specify the ".java" in JAVAFILE explicitly. If your code is syntactically incorrect, rather than producing class files, this command will output error messages telling you why it could not compile the code.
java CLASS
run the java class CLASS. You specify the class and not the class file here in CLASS, so CLASS should not end in ".class". For this to work, the class needs to be declared public, have a public static void method that takes an array of strings as its single argument, and must have been compiled to Java bytecode already with the javac command.

Emacs commands you will need

Emacs is a very sophisticated text editor. It has tons of capabilities, but you can just use it as a simple text editor with a few simple commands. If you want to change the emacs text and background colors, ask me and I'll show you how. Emacs commands show in the bottom line of emacs and this is where you'll specify the arguments to those that take any. Emacs makes backups by saving the previous version of any files it changes as the same file name with a tilde ("~") afterwards. You can select regions of text with the mouse, or with CTRL-space described below.

CTRL-X CTRL-S
save the file. Note that if the currently displayed file has been modified (is different than the file on disk), emacs will display an asterisk ("**") in the lower left corner.
CTRL-X CTRL-C
exit emacs. Emacs will ask you if you want to save any files that have been modified.
CTRL-X CTRL-F FILE
find file. This will change the current buffer that emacs is editing to FILE. If FILE does not exist, emacs will create it. Note that the other file(s) you are editing are not unloaded, but just stored in the background. If you pull down the "Buffers" menu, you'll get a list of current files in emacs from which you can select the one you want to pull to the front.
CTRL-SPACE
mark beginning of region. The end of the region will be where the cursor is when you next use a command that requires a region (like CTRL-W). You can also select regions with the mouse
CTRL-W
Remove (cut) the selected region and copy it onto the clipboard. Note that there is a "copy" option in the edit menu, but it is usually faster to cut the region and re-paste it with CTRL-W CTRL-Y than to actually use the mouse and navigate the menus.
CTRL-K
cut line and copy to clipboard. Multiple lines cut in a row by repeated CTRL-K's will be copied together to the clipboard and can be pasted with one CTRL-Y command. If you cut like this, you'll find yourself pressing CTRL-K twice per line, as the first does not cut the newline at the end of each line and second ends up cutting just the newline.
CTRL-Y
paste most recent region copied to clipboard. To paste regions copied that are not the most recent copied, go to "Select and Paste" in the edit menu.
CTRL-G
cancel command (panic). If you start an emacs command and make a mistake, hit CTRL-G to exit command mode.