Page 1 of 5
Modern Java Contents
- Getting started with Java
- Introducing Java - Swing Objects
- Writing Java Code - Methods
- Command Line Programs
- The Java User Interface - More Swing
- Working With Class
- Class Inheritance
- Building a Java GUI - Containers
This chapter is a complete beginners introduction to Java's control statements - the if, if else, for, while and do while.
If you have programed in any other language you will find many of the idea explained at great length in this chapter very obvious - because once you have seen such things in one language you have seen them in all languages. If you are in this position skip this chapter and read the more advanced condensed summary of the same ideas- coming soon.
Although it is very easy to focus on objects, properties and methods the fact of the matter is that at some point you have to write a lot of Java commands to make an application work - that is you have to create some "code". The beginner using a modern approach to creating a user interface via the drag-and-drop designer can avoid writing much code for a long time - but there comes a time when you have to do more.
Creating code that does something is the traditional core of the programming skill - today it often gets lost in many other considerations.
In this chapter we are going to look at the fundamentals of creating lists of commands that get the job done. If you are a skilled programmer in another language you can certainly skip this part as it is for the complete beginner. It might not be the glamorous end of programming but it is essential. On the other hand this isn't a completely "dumb" introduction to the ideas and you might find it worth a skim read even if you are an ace coder.
A Program is?
A program is a list of instructions. The computer simply reads the list of instructions in the order that you present them and obeys each one in turn.
In the days before objects, properties, methods and events were invented that really was all there was to programming. You started up a text editor, typed in lines of code - i.e. instructions in what ever language you were writing in - and then ran the code. There was no need to worry about objects, properties, methods or, in most cases events.
Today what a program does is mostly determined by the code contained within methods. A method is simply a chunk of code that belongs to an object. It is something the object can do. For example, a button might have a method called disable which would set the button into a disabled state when used.
Some methods are special in that are activated in response to an event. For example, the button's click event handler is a method that is automatically obeyed when the user clicks the button.
The key idea is that we have to write code in methods to make something happen. Let's look a the sort of things that we can write in a very general way.
Variables, assignment and expressions
Before we can move on and look at instructions - the verbs of the program - we have to have something for them to work with - the nouns of the program. As we have already discovered the nouns of Java are objects with properties and methods but there is a more fundamental entity that we have to consider - the variable.
A variable is roughly speaking a chunk of storage that you have given a name to. What you can store there depends on what you specify when you create the variable.
For example, in Java, and most modern languages you can store an integer (whole number) value in an int variable (int is just short for integer).
So for example
creates a variable called a that you can store an integer in.
How do you store a value in a variable?
You use the assignment operator "=". For example:
isn't a statement that a is equal to ten, it is an instruction to store the value 10 in the variable a.
Understanding what is going on becomes much easier if you always read the equals as "assign to" as in "assign 10 to a" or "store in" as in "store 10 in a". (The point is never read it as "a equals 10" because this could be a statement or a question.)
A variable can only store one value at a time so if you store another value in it then the anything already stored is lost.
stores 20 in a and any record of the 10 previously stored is lost.
Most programmers who have been working with variables quickly forget that there is any other way it could be done.
If you are happy with these basic ideas then we can add two more observations. You can both create and store a value in a variable at the same time. That is instead of writing:
you can write
It's just a shorthand.
The second idea is slightly more complicated. You can use a variable on both sides of an assignment. For example,
this retrieves the value stored in b and stores it in a. After this instruction a and b store the same value. (Again it is important not to read this as "a equals b" but "store the contents of b in a" - its a command not a question.)
You can also write things like:
and the right hand side, an arithmetic expression, will be worked out and stored in a. Notice you can use variables on the right hand side and their values will be retrieved and used in the calculation.
You can generally write arithmetic expressions as you would normally but notice that the multiplication sign is "*" an asterisk and the division sign is /. Also feel free to use brackets to make sure things are worked out in the correct order. More on arithmetic and other types of expression later.
An arithmetic expression that causes a lot of problems when you first see it is:
Which if you read it as "a equals a plus 1" sounds like nonsense. What is does is to increase the value stored in a by one - i.e. its increments the value in a. If you read it as "retrieve the value in a, add one to it and store the result back in a" - then all should be clear.
Adding one to a variable is so common that Java has a special command for it:
is the same as a=a+1;
There are lots of different types of data that can be stored in a variable but for the moment integer variables will serve well as an example.
Now we have something to write instructions about lets see what sort of things we can do.