Java development for beginners 2. Variables, expressions and data types.

This tutorial is the first part of two tutorials in which we are going to define the words; variable, type and expression.

Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides. The data is given a name, so that it can be re-called whenever it is need.

In Java, all variables must be declared before they can be used. The basic form of a variable declaration is:

type identifier [ = value];

The type is one of Java's datatypes. The identifier is the name of the variable.

You have to take into account;

- Variable names can't start with a number. So dog is OK, but not 1dog. You can have numbers elsewhere in the variable name, just not at the start.

- Variable names can't be the same as Java keywords.

- You can't have spaces in your variable names. The variable declaration int dog teeth will get you an error.

- It's a common practise to have the first word start with a lowercase letter and the second or subsequent words in uppercase: dogTeeth, myDogTeeth

- Variable names are case sensitive. So dogTeeth and DogTeeth are different variable names.

The datatype is important to inform Java over the amount of memory space it is going to need to keep that variable.

Below we have the code we are going to use for the example;

public class Variable {
	public static void main(String[] args) {
		int a;
		a = 1;
		int b = 2;

		System.out.println(a);
		System.out.println(b);
		

		System.out.println("end");
	}
}

<< Previous Next >>