Lecture 2

The Basic Building Blocks of a Java Program

When we describe the world to a computer, we have to reduce it to things that the machine can understand. In object oriented programming we make the abstraction that the world consists of objects. For a computer, however, an object is nothing else but a piece of memory that stores the values of its attributes. An important observation in OOP is that many objects are very similar to each other and knowing something about one of them already tells us a lot about the other one. In order to capture the similarity and to describe the commonalities once and for all, we group the objects into classes. For instance words like 'computer' or 'book' actually refer to a class of objects rather than a particular object. At the same time the expression 'my computer' refers to a particular object which is of class 'computer'. When we talk about a particular object of a given class, we often say that the object is an instance of the class, i.e. 'my computer' is an instance of 'computer'.

To write a program in Java, means to write a class definition. Let's start with our first Java class which will be the basis of a program:

// This is the definition of the class for our first program
public class First {
}

The first line above is just a comment for humans. Any line starting with '//' or any text between the symbols '/*' and '*/' is ignorred by the computer and this is used by the programmers to document their code. It is a good coding style to document your programs. The next two two lines declare a class called First.

You have to put the code for the class in a file with the same name as the class name and with the extension .java. In this case the file should be called 'First.java'. You can use any text editor to create the file but note that some editors that are sometimes desribed as "text editors" or "word processors" do not really produce what the programmers understand as text. For instance Microsoft Word and Open Office do a lot more than just producing text. You should avoid using those for writing programs. What you actually need is an editor like Emacs, Geany, GEdit, Notepad or Notepad++.

As we have discussed, Java is a high-level programming language which cannot be executed directly on a computer. We first have to compile it. For that purpose we use a compiler. This is how we run the compiler for Java:

javac First.java

You have to open a terminal window, go to the directory where you have saved the First.java file and then run the command above. If the compilation is successful the compiler will not show any message and will just silently complete its work. If you have some errors, however, you will see this as a message in the terminal. After the successful compilation, you will find that there is one more file in the same directory called 'First.class'. It contains the sequence of JVM instructions.

There is very little to be explained in the code above. It contains the name of the class preceeded by the keywords 'public class' that tell the compiler that this is a class definition. The currly braces '{' and '}' mark the beginning and the end of the class definition. For now we just leave it empty but later we will add everything that we need between the braces.

Now we are almost ready to run our program, there is only one more detail to make clear. Programs compiled to native machine code can be executed directly by typing the name of the application. However, as we said, Java is specifically designed to be platform independent and for that purpose it is compiled to JVM instructions rather than to the native instructions for the CPU. For that reason we must run a Java program via the Java interpreter. This is how we do it:

java First

Unfortunately, the effect of running the program is not very impressive. We just get the following message in the terminal:

Exception in thread "main" java.lang.NoSuchMethodError: main

The message looks quite cryptic but it is fortunately easy to explain. So far we have just defined a class called First and nothing else. Java loads the class but it doesn't know what to do with it. We need to say what we want our application to do. The behaviour of objects and classes in all object oriented languages is defined by defining methods. The method is like a receipe for achieving a specific goal but is described in the programming language. You can also hear the terms "function", "procedure" or "subroutine" which are terms similar to "method" in Java but are more popular in other programming languages. When we run an application associated with a given class then Java looks for a method with the name 'main' in the class. This is how we must define it:

public class First {
     public static void main(String[] args) {
     }
}

There are only two important things here. The name of the method, i.e. 'main', and its definition. The definition of the method is everything that we put between the currly braces. For now we don't put anything there so the method will be defined but it will not do anything. The other things that you see in the code, i.e. the keywords 'public', 'static' and 'void' and the definition (String[] args) are not important for now and they will be explained later. Now if you try to run the program again with:

java First

The interpreter will not show an error anymore but it will just silently finish its work. You don't see an error but you don't see anything else either. It is time to make our program do something:

public class First {
     public static void main(String[] args) {
        System.out.println("My First Program");
     }
}

The new program will print the message "My First Program" in the terminal and after that it will finish its work. For now you can think about the line between the curly braces as a command to the interpreter to print a message. The message itself is the text inside the double quotes. Later we will see that 'System.out' is actually a globally defined object which lets the application print messages in the terminal. The printing itself is done by calling the method 'println'. The message itself is an object of its own. The Java compiler understands that for any text enclosed with double quotes it has to create an object representing the text. In this particular case the program will create an object representing the text "My First Program". This object is nothing else but a piece of memory which contains the message encoded as a sequence of zeros and ones. Once the object is created, it will be passed to the method 'println' of the object 'System.out'. The method will take the string and it will print it to the terminal.

NB: Did you notice the semicolon ';' at the end of the call to println? Every statement in Java must be terminated with a semicolon. It is a common mistake to forget it!

Using Variables

In the last lecture we introduced variables and the assignment statement. This is a more concrete example of how they are used in Java:

public class Variables {
     public static void main(String[] args) {
        int i = 0;
        System.out.println(i);
        i = i+1;
        System.out.println(i);
        i = i+1;
        System.out.println(i);
     }
}

There are a couple of new things here. First note the keyword int before the first assignment statement. As we said, every variable denotes a memory cell. Java, however, is more pedantic and it wants us to declare how the binary number in that cell is going to be interpreted. For now we will mention only two keywords int and double. By using the keyword int we declare that we want an integer, i.e. a number like 0, 1, 2, ... On the other hand double means a real number, i.e. a number with fractional part like 3.14, 2.718, etc. We must always declare a variable with int or double before we are allowed to use it. In the example we declare that i is an integer variable and we intialize its value to be zero. We print the value of the variable by using println. After that we increment the variable twice and each time we print its new value.

We have also pedantically put semicolons at the end of each statement. Without it this would not be a valid Java program. In the last lecture we have been a bit sloppy and we didn't use the semicolon when we talked about the assignment.

Quickly About Loops

Programming would be rather useless if we were only able to perform a static number of steps. The power of computing comes when we are able to ask the computers to repeatedly do the same operation over and over again. This is something that humans find boring and is actually better to delegate to a machine. This is possible by using loops.

The most powerful and the one that you will need in the first lab is the for-loop. For example if you want to print a message not only once but 10 times then you can do it like this:

public class Second {
     public static void main(String[] args) {
       for (int i = 0; i < 10; i = i + 1) {
         System.out.println("My Second Program");
       }
     }
}

The purpose of the for-loop is to execute whatever is between the curly braces several times. Note that in the parenteses after the keyword for there are three statements. Before the first iteration of the loop, the Java interpreter will execute the first statement after the opening parenthesis, i.e. int i = 0. After that the condition i < 10, i.e. the second statement, is checked. The operator < stands for "less than" just like in math. If it happens to be true that i is less than 10 then the body of the loop is executed, i.e. the println statement. At the end of each iteration, the interpreter also executes the third statement in the parentheses. In this case it increments the value of i. This means that in the first iteration i will be 0, then 1, 2, 3 ... and finaly 9. In total the body of the loop will be executed ten times.

In the first example we used the loop to just repeat one and the same operation many times. We can do more than that. Since we have a variable like i we can use it to do slightly different things on each iteration. For example, if we want to compute the sum of the numbers from 0 to 9, then we can use a loop like this:

int sum = 0;
for (int i = 0; i < 10; i = i + 1) {
    sum = sum + i;
}
System.out.println(sum);

Here we first define and initialize one variable called sum. After that on each iteration we add to it the current value of i. The execution of the loop can be illustrated with the table:
isum
00
10
21
33
46
510
615
721
828
936
45
Each row of the table shows the current values of variables i and sum just before the execution of the third line in the example. The last row shows only the final value of the variable sum which is 45=0+1+2+3+4+5+6+7+8+9.

Defining Methods

It is unavoidable that once we have programmed a computer to do something we would like to ask it to do the same thing at different times and in a different contexts. For that purpose every large program is built from small blocks where each block does only a small and well defined task. In Java we call these building blocks methods. As we said a method is like a receipe for a specific computation.

We have already programmed examples with a single method called main, but we can have more than one method. The following example defines a method called sumUpTo which takes as parameter a number n and returns the sum of all integers from 0 to n:

public class Sums {
    public static int sumUpTo(int n) {
      int sum = 0;
      for (int i = 0; i < n; i = i + 1) {
        sum = sum + i;
      }
      return sum;
    }

    public static void main(String[] args) {
      for (int i = 0; i < 10; i = i + 1) {
        System.out.println(sumUpTo(i));
      }
    }
}

There are four essential components in the definition of a method. The body of the method is the code that is included between the two currly braces '{' and '}'. When we wrote the main methods above, we cared only about its body.

The other obvious component is the name of the method. What exactly name we choose for the method is completely irrelevant to the Java compiler, but it is essential for us as programmers. A good name would help us to remember the name and it will immediately give us some intuition about what the method might be doing. The main method is called main because it is the main starting point of a program. The Java interpreter knows that it must look for it in order to start a new program. For the other method we choose the name sumUpTo to remind us that it can be used to compute the sum of all integers from zero up to a given number n. We could also have chosen the name sumFromZeroUpTo but we must keep some balance between the length of the name and its clarity. In this case we opted to choose a shorter name. Note how we follow the convention to write the name without spaces between the words. Instead we use capital and small leters to separate the words. The reason is that Java let us to use only small and capital letters but not spaces. After the first letter of the name we are also allowed to use digits and underscores "_". The underscore is also sometimes used to separate words within a name.

A method can also take one or more parameters. In our example the parameter is n and it specifies the upper limit until which we must do the summation. The list of parameters is always written in parentheses after the name of the method. Each parameter definition consists of a type, i.e. int or double (for now) and a name. In the example the parameter definition is int n. The main method also has a parameter defined as String[] args. Here args is the name and String[] is its type. What exactly String[] means and how this parameter is used we will see later. If there are multiple parameters then they are all enumerated and separated by comma ",". If there are no parameters at all then we just write a pair of empty parentheses.

A method may also return a result. This is indicated by the type immediately before the name of the method. If it is int or double then the method must return respectively an integer or a real number as a result. In the example the method sumUpTo must return the sum of the numbers and therefore its result type is int. The main method does not return any value and this is indicated with the keyword void before the method name. In other programming languages, methods that return values are often called functions. On the other hand methods marked with void are called procedures or subroutines.

What exactly is the returned value of the method is indicated with the return statement. The behaviour of return is to immediately terminate the execution of the current method and to return back to the method that has called it. If the method returns a value then the return method is immediately followed by an expression whose value will be returned back to the calling method. If the method is marked with void then return cannot be followed by an expression.

We can call a method at any place where an expression or a statement is expected. In the example the main method calls println with an argument which is computed via a call to sumUpTo. When we call a method then we cite its name followed by a list of arguments in parentheses. In this case we have only one argument and we give it as value the value of i. When we make the call the value of i is copied as a value of the formal parameter n in the definition of sumUpTo. In this way we can call the method many times where each time it can be executed with different parameters. This lets us to reuse code while we are still able to parametrise the different calls in different ways.

Exercises