Return to lecture notes index
15-100 Lecture 2 (Wednesday, August 30, 2006)

What is a Program?

To some unfamiliar with software development, the word program conjures an understanding of something cryptic, complicated, and complex by nature. In fact, nothing is farther from the truth. A program is nothing more than a document.

For our purposes, a program is a document written by a person to be read by two different audiences: people and machines. Programs describe software systems. Sometimes these systems perform services for people, services such as word processing financial management, &c. In other cases they are primarily models of real-world systems, such as those programs used for astronomy research, predicitng economic markets, or estimating the weather.

People read programs for several reasons. One scientist might read another scientist's program, for example, to learn about the real-world system that it models. This idea is actually familar to us in a different context -- we've all seen scientific knowledge communicate through simple equations or systems thereof. These "formulas" distill learned relationships into a very concise, rigorous form. Programs which model interesting systems can serve exactly the same purpose.

Programmers might also read programs to improve their own ability to solve problems by computer. There hardly exists a good writer of any kind who isn't well read. The best writers have their own techniques -- influenced by many others. In a similar way, programmers learn by reading other programs. You'll read many of our programs this semester and, by reading them and writing your own, your programming skill will improve.

Programmers also read other programs for the sake of understanding the details of how they are written and how they work -- with an eye toward improving or changing them. A programmer who is asked to fix a "bug", to improve a program, or to change a program's features to match new requirements, will usually spend significant time reading a program before changing it. This shouldn't be suprising -- can a book editor make changes before reading the work?

So, as people, we read programs to learn how they work, to improve our skills, and to learn about the systems that they represent.

Person vs. Machine: Closing the Gap

When people write programs, they are doing it with a mind toward solving some real problem. They want to be concerned with the problem and its solution, not the details of the machine that is a tool for solving it.

For this reason, programmers usually write programs in English-like programming languages: C, C++, and our favorite, Java, for example. Although these languages are structured in a way that is useful to a computer, they are designed to be understandable and convenient for people.

Ultimately, a program written by a computer in one of these languages, a so-called High-Level Language is translated into another form that is better tailored for the machine. This new form is often known as an assembly language program or, in Java, byte code.

To help understand the difference, I like to think about a car with a driver and passenger. The passenger might give the driver directions:

Back out of the driveway and go right. Continue for 4 blocks. At the stop sign, make a right. Travel about 1/2 mile. You'll see a grocery store. The parking lot is on the right-hand side of the road, just past that grocery store. Park there.

The passenger provided a set of high-level instruction to the driver. These instructions were provided in a way that the driver understood and used commands that were descriptive in the context of the problem: Navigating the city en route to the grocery store.

But the car, the machine, can't understand these instructions. It requires instructions in a different language. The instructions might begin like this:

Press break pedal: Not less than 25 lbs of pressure. Push key into keyswitch, twist forward to on position; listen for click. Twist forward again with not less than 10 lbs of pressure. Listen for motor. Release keyswitch pressure. Slide gear selector down two notches into reverse. Reduce brake pressure to 5 lbs. Roll to bottom of driveway. Rapdily increase break pressure to not less than 25 lbs.

Etc. Etc. Etc.

The driver has to translate the high-level instructions provided by the passenger into a low-level (physical) language understood by the car. This language is coposed of much smaller steps. And, it might vary slightly from car to car. For example, the appropriate actions taken by the driver will be different for a car with a "standard" transmission than one with an "automatic" transmission. So, although the passenger's language is problem-oriented and car-independent, the driver's translation is car-oriented and car-specific.

The programming process happens in much the same way. Human programmers produce high-level directions in languages like Java. In this respect, we are acting like the passenger. These documents are then given to a program called a compiler that acts like the driver and translates them into a language appropriate for the machine.

The resulting program, which is machine specific, can run on only one type of computer, such as iMacs running OS X. This is why, when you go to the store, you have to buy different versions of the same program for different types of computers. The same high-level programs were compiled for different types of computer.

Java and the Virtual Machine

The designers of Java wanted to take a different approach. They wanted a programming system which was "compile once-run anywhere". In other words, they wanted one set of machine-oriented instructions to work on all computers.

This solution obviously can't work -- at least without something else entering the picture. Why not? Different computers understand different low-level instructions.

So, here's what they did. They invented a new type of computer, the Java Machine. And, they wrote a compiler that would convert Java high-level programs into low-level programs that would only run on this Java Machine.

But, they didn't build this machine in hardware. Instead they wrote a program to model it in software. Running this program creates the so-called Java Virtual Machine (JVM). The JVM was then compiled for many different types of physical computers. This software was then made available for many systems: It is part of the jdk that you might ahve downloaded to run java from home.

So, here's the Java model. The JVM is written and compiled for many different systems and is distributed widely. Java programmers compile software for the JVM. When one wants to run a Java program, one starts the JVM program and lets it run the compiled Java program.

Our First Program

In class, we created the following Java program using a simple editor, vi. It was saved as "HelloWorld.java". It could actually have been created with any editor that can save plain text files -- remember, programs are just documents. As "for examples", we could have used emacs, pico, notepad, MS Word, &c.

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

It isn't hard to guess what this program will do -- it prints the line "Hello World!" to the screen. "System.out" is the output subsystem. and println() is short for "print line". Will make more use of this soon enough.

For right now, just observe this -- you've never programmed before, but you know what this program does. Remember, this is by design. People are one of the two audiences for programs.

Command-line Tools

We can compile this program using the Java compiler. It shouldn't be surprising that this program is named, javac. Here's how it works:
javac HelloWorld.java
If we look in the file system, we now see the byte-code -- the low-level program for the JVM. The byte code has the extension ".class".
ls HelloWorld.class

We can start java programs by providing the class name, without the extension, to the JVM. This is done, again no great surprise, with a program called "java" as follows:

java HelloWorld

The above example prints, as intended, "Hello World" onto a console.

Making it Happen

The process used to create the "HelloWorld program" involved three steps: creating the Java program, compiling it, and starting it. If it had been a more complex program we might also have had to manage multiple pieces or debug mistakes.

In class, we went through and repeated the creation of the "Hello World program" using SubEthaEdit, javac, and java. Although we chose SubEthaEdit in class, any text editor that saves plain files will do. Just steer clear of things that don't save in plain text: Warning! Apple's simple text editor is one of these. Many folks like Dr. Java, Eclipse, or Blue Jay.

We compiled the program at the commandline: javac HelloWorld.java

Then, also from the command line, we ran it: java HelloWorld