Writing Java Code - Methods
Written by Alex Armstrong   
Thursday, 20 January 2011
Article Index
Writing Java Code - Methods
The flow of control
Conditionals - if
While
Try it out

Using ifs and loops is one of the most difficult parts of learning how to program. Our beginners introduction to Java reaches the part all programmers have know and know well - how to write code.

 

cover

Modern Java Contents

  1. Getting started with Java
  2. Introducing Java - Swing Objects
  3. Writing Java Code - Methods
  4. Command Line Programs
  5. The Java User Interface - More Swing
  6. Working With Class
  7. Class Inheritance
  8. 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.

Code

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

int a;

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:

a=10;

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.

For example:

a=20;

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:

int a;
a=1;

you can write

int a=1;

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,

a=b;

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:

a=2*4+b;

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:

a=a+1;

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:

a++;

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.

<ASIN:0132354799>

<ASIN:1430234164>

<ASIN:0596009070>



Last Updated ( Wednesday, 07 November 2012 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.