Computers have been around us for many years now. They are placed on our desks, inside our phones, and inside our cars and washing machines. Computers do their work by following instructions. These instructions are bundled together, as computer programs. The programs are then executed, which means that the instructions are followed, sometimes one by one and sometimes more than one at the same time, and the computer does what the instructions tells it to do.

The act of creating computer programs is referred to as programming, or if you wish to make it more clear, computer programming. This book has the goal of giving you an introduction to computer programming. It will do so by describing the basic elements of a programming language, and how these elements can be used to create programs.

Before learning how to do the actual programming, it is important to gain experience with the practical usage of computers. I will assume that you have such practical experience, and most likely you are using a computer, or perhaps another type of electronic reading device, right now, as you read this text.

If you read the book on the web, you are typically using a browser program, such as Firefox or Opera. If you use another reading device, such as a tablet computer, you may be using some kind of reader software, like a Kindle reader program or an iBooks program.

There are many types of computer programs, used in different scenarios, such as programs for word processing, program for making numerical calculations, programs for reading e-books, and programs for controlling the speed of your car or the temperature of your washing machine.

The programs described in this book will be small compared to a browser program, and they will also be small compared to a car control program, but they might still be useful for learning the important concepts.

The book describes how to write the program text, also referred to as source code. In order for a program to run, which is another word for execute, the program must be represented in a format that is understandable by the computer. This format is referred to as machine code. This means that the source code must be translated into machine code.

Although the book concentrates on how to write the source code, there will be references given, to places where more information on how to make actual programs run on your own computer can be found. It is advisable to follow along, and to make your own programs, as you read the book.

This is a Book with Views. This means that the book covers several programming languages at the same time. When you read the book, you read it one view at a time, and you can easily switch betwen the views. In the e-book version of the book there are links to the views at the end of each section, and in the web version of the book there are also links to the views in the left sidebar of each page. The view you are currently reading treats the programming language Java.

The book is written to give an introduction, and to give possibilities to practice writing programs. When doing this, you take on the role of a programmer. Perhaps you will do it as a hobby, or perhaps you will do it as a profession. In any case I hope the book might be of help in the task of learning programming.

The remained of this chapter will give an introduction to programming, in the form of a simple example. In the following chapter Names and values, we will start looking at variables.

Say hello

It is customary to write a small program first, before making larger programs. A small program, with its only task being the display of a text message, saying "Hello, world", is often used. Such a program can be referred to as a Hello world program.

A Hello world program written in Java often starts with a class definition. A class can be thought of as an aggregation of instructions and data, put together for a specific purpose. The instructions inside a class are typically divided into groups, each group in turn having a specific purpose. Such groups are referred to as methods.

The class definition here starts with the line

class Hello

which gives the name Hello to the class. In this example, the class Hello will be the main class used in the program, and the only class created by the programmer. An already defined class, named System, will also be used, for the purpose of getting access to a method called println. This method, which is used in the program as

        System.out.println("Hello, world"); 

makes the string Hello, world appear on the screen of the computer where the program is run. The complete program is shown in Figure 1.

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

Figure 1. A hello world program in Java.

This the Java view - other views are C - Python

The program in Figure 1 contains a method named main. The purpose of this method is to define a starting point, so that the operating system knows where to start the program when it is executed.

Read more

How to make it run

The program in Figure 1 can be executed on a computer. I assume that you have a computer, perhaps a computer looking like the computer in Figure 2.

Figure 2. A computer, of a possible type that you may want to use for programming. It may also be a computer in the form of a mobile phone, or some other device where a computer resides inside.

When you program using the language Java you need a compiler. You also need a Java virtual machine. It is possible to obtain a compiler and a Java virtual machine by using the Java SE Development Kit, which is available for Windows, Linux, and for Mac OS.

Read more


This book has been produced using several open source software products and open standards. It is written using XML, and translated, using Python software, to html for the web version, epub for the e-book version, and LaTeX to be converted to pdf for the print version. The mobi version is currently created using Calibre. Emacs and PSGML are used for editing. The book contains images from the Open Clipart Library.

Read more