Programming a computer simply means telling it what to do. This can be done in a variety of ways. We could design a circuit to solve a specific problem. We could write assembly code which told the computer exactly what memory to access and which bits to add and subtract. We could write a high level code in a language like C, Fortran, or Java which is then passed through a second program (called a compiler) which interprets our commands.
In order to make programming simple on each different computer, and to focus the programmers efforst to solving their problem and not learning about the specifics of the computer they are on, almost all programming is done using these high level languages.
Your first program - Hello World
As with most programming tutorials, we are going to start with the classic example of having the computer say hello.
So how do we do it?
We have to
Write the program
Compile the program
Run the program
Writing the program
Create a file HelloWorld.java, and begin editing it. (In UNIX, at a command prompt type "vi HelloWorld.java".) Type in the following text
// The main unit in java is the "class"
// A class is a set of stored values, and a set of defined
// operations to be performed on those values.
// Our program HelloWorld will be defined as a class.
public class HelloWorld {
// main routine is what "runs" the program
public static void main(String [] args) {
//print message to screen
System.out.println("Hello World");
} // end main routine
} // end class HelloWorld
Notice a number of things about this program.
First, we start with a number of very general descriptions, all starting with a //. These are called comments, and they are not part of the code per se, but merely a description by the programmer of what she or he is doing. Commenting your code is very important. You may come back to your code a year later and need to remember what each section of code does, or, maybe a second programmer will need to modify your code.
Second, we use indentation throughout the code. The first non commented line of code and the last non commented line of code are not indented. The first non commented line starts a class called HelloWorld, which ends with a curly bracket. Sets of curly brackets in Java contain sections of code. The HelloWorld code is contained between the beginning and ending curly brackets. The first non comment line of HelloWorld is the beginning of the main routine. main is just what it sounds like, the main body of the program. main also has a set of curly brackets to set aside those lines that are a part of main. Notice that the lines to begin and end main are indented once, to denote that main belongs to HelloWorld. All of the lines within the main routine are indented a second time to denote that they belong to the main routine of HelloWorld. These indentations do not actually do anything to the code, but they do make it much easier to read, especially when the code starts getting complex.
Allright, so we have a bunch of commands, but what do they do?
public class HelloWorld {
starts the definition of the code. It tells the computer, I am going to start telling you what the code is that is associated with the HelloWord class. The word public refers to the fact that the code can be accessed from "outside" the class. You want to be able to run the code from the computer, so it is a good idea for the main code to be public. Some elements you might want to keep private, and we will talk about that later.
public static void main(String [] args) {
starts the main routine. It is public, once again, because you do want people to be able to run the main routine. static refers to the fact that the main routine exists whether or not someone has told the computer to set aside memory for an instance of our HelloWorld class. We will cover what static means in more detail later. For now, just realize that the main routine has to be static.
System.out.println("Hello World");
prints a message to the System's output device, the screen.
} // end main
ends the main routine, it is the close bracket that pairs with the main routines open bracket. Curly brackets close in the reverse order that they are opened. It is similar to nesting parentheses in algebra.
} // end HelloWorld
closes the HelloWorld class.
Compiling the Program
Let's compile it. In UNIX, save your file and close it. At a command prompt, type the following: "javac HelloWorld.java". If the code is written with no errors, you should create a file HelloWorld.class (type "ls" after the compile command completes to see if the class file was created.) If there are errors, you should get a list of them. Each one should include a line number. Start with the first line on which there is an error, and correct your file. Keep fixing errors and recompiling until you do not get any errors.
Running the Program
To run a program in Java, we have to take the class files, and run them through the machine interpreter. In UNIX, do this by typing "java HelloWorld" at a command prompt.
Did your code work the way you expected to?
You should get something like the following result:
[localhost:~/Intro/HelloWorld] djoiner% javac HelloWorld.java
[localhost:~/Intro/HelloWorld] djoiner% ls
HelloWorld.class HelloWorld.java
[localhost:~/Intro/HelloWorld] djoiner% java HelloWorld
Hello World
[localhost:~/Intro/HelloWorld] djoiner%