Page 1 of 6
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 to know and know well - how to write code.
With NetBeans And Swing
- Why Java?
- Getting started with Java
- Introducing Java - Swing Objects
- Writing Code
- Command Line Programs
- User Interface - More Swing
- Working With Class
- Java Class Inheritance
- Java Data Types - Numeric Data
- Java Data Types - Arrays And Strings
- Building a Java GUI - Containers
- Advanced OOP - Type, Casting, Packages
- Value And Reference
- Java Lambdas, SAMs And Events
This chapter is a complete beginners introduction to Java's control statements - the if, if else, for, while and do while.
If you have programmed in any other language you will find many of the ideas 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 or read the condensed summary of the same ideas at the end just to make sure you do know it all. 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.
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 instructions 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. It might not be the glamorous end of programming but it is essential.
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 and might even wonder why I'm bothering to tell you all this.
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.