Modern Java - Writing Code
Written by Mike James   
Article Index
Modern Java - Writing Code
Expressions
Conditionals
If Else
While
Project

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.

Modern Java
With NetBeans And Swing

largecover

Contents

  1. Why Java?
  2. Getting started with Java
  3. Introducing Java - Swing Objects
  4. Writing Code
  5. Command Line Programs
  6. User Interface - More Swing
  7. Working With Class
  8. Java Class Inheritance
  9. Java Data Types - Numeric Data
  10. Java Data Types - Arrays And Strings
  11. Building a Java GUI - Containers
  12. Advanced OOP - Type, Casting, Packages
  13. Value And Reference 
  14. 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.

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 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

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 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:

int a;
a=1;

you can write

int a=1;

It's just a shorthand.

<ASIN:1430234164>

<ASIN:0596009070>