Recent Changes - Search:

Java Tutorial

edit SideBar

IntroductionToProgramming

These pages make up the course notes for my Java programming course (run at Dallam, Milnthorpe, Cumbria). Hopefully they also make a useful self-learning tutorial.

A computer needs hardware and software to make it work. Hardware is quite easy to understand, it is the things you can see (the “box”, monitor, keyboard, mouse, etc), but on it’s own the hardware would not be of any use; it would not do anything. To make use of the hardware we need to provide it with instructions about what we want it to do. These instructions are known as Software.

At a very deep level the hardware uses electrical signals to make things happen. For convenience we like to represent these electrical signals as numbers , which in turn represent instructions (known as machine code), addresses or data. Even when we have made the leap from electricity to numbers we are still not a lot better off. What we would like is a way of giving the hardware instructions in something like a written list – a bit like a recipe for example:

	Fill the kettle with water
	Switch on the kettle
	Take a Cup
	Put a teabag in the cup
	When the kettle has boiled pour water into the cup
	Stir and remove the teabag
	Add Milk

This is what programming languages aim to achieve; a programming language is a specific way of writing something akin to natural language which can be converted to a form that the hardware can use. And that’s the clever part. We can use some software to translate from the programming language to the machine language for us. As long as we use language that the translation software can understand, then it can create the machine language.

Think of it as having a person who only speaks English wanting to communicate with a person who only speaks Cantonese, they need a person who can translate for them. Assume that the translator only understands certain words and phrases of each language, as long as they are careful about what they say, they can communicate.

Consider the following phrase:

 “understand this you can”

You know that that phrase really ought to be “you can understand this”, but either way you understood it. However the translator with limited knowledge of English may not understand that phrase.

This is similar to the situation with programming languages. The software that does the translation has a very limited vocabulary and expects things to be very precisely specified. There are two approaches to doing this translation – i.e. two categories of programming language, known as Interpreted and Compiled. Java – the language we are going to learn is a Compiled language (you need not bother too much about this distinction at this stage).

Here are all the words that the Java Compiler understands:

abstractdefaultgotopackagesynchronized
Assertdoifprivatethis
Booleandoubleimplementsprotectedthrow
Breakelseimportpublicthrows
Byteenuminstanceofreturntransient
Caseextendsintshorttry
Catchfinalinterfacestaticvoid
Charfinallylongstrictfpvolatile
Classfloatnativesuperwhile
continuefornewswitch 

As you can see it is not very many compared to all the words commonly used in English. The Compiler also expects some “punctuation” in what we write, and like a strict English teacher it will refuse to accept any incorrect punctuation. Also the caseOfTheLettersMustBeCorrect. So if the compiler is expecting “String” then string, STRING or striNg will not do.

Lets take a look at a simple program, the one that most tutorials start with is making the computer say “hello”. To do this in Java we have to input the following:


// A program to say hello
public class SayHello {

        public static void main(String[] args) {
                System.out.println("Hello World!");
        }//end of main method

}   // end of class SayHello

As an aside the content of a program is also known as code.

The first thing you may notice is that the first line starts with a //. Anything on the line after // is ignored by the Compiler. So why bother? This is known as a comment and it is there to remind you, the person writing the program, what the program does. Good Practice says that we should put lots of comments in our code (program).

The next line is important to the compiler, Java expects almost all instructions to be inside a class (because Java is an Object Oriented language – more on this later) and this line says what the class is called. It also says that the class is “public”, which means that it can be used by anyone (almost all classes that you write will be public). So the format of this line must be:

	[scope] class [classname] {

Where scope is either public, protected or private and classname is a name that you made up to describe this class. The BRACE (curly bracket { ) marks the start of the contents of the class. There is a corresponding closing brace ( } ) on the last line to indicate the end of the class. Notice there is also a comment on that line to indicate the end of the class. This is good practice as we use lots of braces it is often hard to figure out which closing brace goes with which opening brace.

A class is a fundamental unit of Java code and a public class must be stored in a file of the same name, so our SayHello class should be stored in a file called SayHello.java

Inside our class there is a method (if you have used other programming languages may think of this as a function, subroutine, procedure …). Methods are things that the class can do. Importantly, for our program to run it must have one method called main. When the programme is run it is the main method that gets executed first. As we sill see later, the main method can call other methods, which may even be in other classes but for now we have only one method and it is called main. We know this because the code says so public static void main(String[] args) {

It is a public method, which means anyone can use it. We will deal with static and void later, then comes the method name main. Methods are always followed by brackets () which may be empty or which may contain some arguments. Again we will go into detail later, for now all we need to know is that for our program to run we need a main method and it must look EXACTLY like it is shown above. Any differences (including upper/lower case letters) will result in our program failing to compile or run.

You will also notice that like the class, the method has braces enclosing the code that it contains. Finally we actually get down to doing something, the line inside the main method

	System.out.println("Hello World!");

Is the thing that actually causes the computer to print Hello World! On the screen.

So to recap, we have a class called SayHello and inside that there is a main method which contains a line of code which makes the computer print Hello World on screen.

 

Creative Commons License

This work is Copyright Chris Hunter 2007, you may use it for non-commercial purposes
under the Creative Commons license Creative Commons Attribution-Noncommercial-Share Alike.

Edit - History - Print - Recent Changes - Search
Page last modified on July 06, 2007, at 02:13 PM