A variable is associated with space in the computer's memory that can hold a value (often a number). Each variable has a name, which you choose, and subsequently use to refer to it.
You must start by declaring the variable, this gives the variable a name and reserves some memory space to store whatever value the variable takes. It also tells the compiler what you intend the variable to represent. This is known as the variable's type. For example a variable which always takes integer values, can be declared as type int in Java. For example the line of code
int i;
declares an integer variable (i.e. a variable of type int) which we have chosen to call i. As an int, it can store any integer value between -2147483648 and 2147483647.
Java provides types to represent several kinds of number,
e.g. integer and floating point, non-numerical things like text, and
other more abstract things. These are listed on page . You
can give a variable a longer name if you like, and it is usually a
good idea to choose a word that explains what the variable is for. The
convention is for variables to be named using lower case letters, or
if the name consists of more than one word, that a capital be used at
the start of each word other than the first. You may also use numbers
or an underscore _ in your variable names, but not at the beginning
of the name. Examples of some well chosen and valid variable names
might be total,
maxValue, answer1.
Exercise 1.2
Now try the simple program below.
As it contains public class VarTry you must type it in to a
file called `VarTry.java'.
Compile and run VarTry in the same way that you did with the Hello program to check it prints out the number nine. Print the output and put it in your lab book. Read the explanation that follows and try to understand how it works.
/** * Name: * Date: * Exercise: 1.2 * Purpose: To demonstrate the declaration and simple uses of variables. */ public class VarTry { public static void main(String[] argv) { int i; // declares integer variable named i i = 9; // gives i the value 9 System.out.println(i); // print the value of i to the screen } }You will see here how each line of code must end with a semicolon, ; , (if you need to write a line of code that is longer than the width of the window you can continue it on the next line by omitting the semicolon until the end of the statement).
The line i = 9 gives the value 9 to variable i. This is known as assigning a value to a variable -- i is assigned the value 9. It means that the space in the computer's memory associated with i now holds this value. The first time a variable is assigned a value, it is said to be initialised. The = symbol is known as the assignment operator.
It is also possible to declare a variable and assign it a value in the same line, so instead of int i and then i = 9 you can write int i = 9 all in one go. If you have more than one variable of the same type you can also declare them together e.g.
int i, j; // or int i=1, j, k=2;which can be a useful way of saving space. Where necessary you should add comments explaining the meaning of the variables, both so it is clear to you if you come to look at your program at a later date, and to those marking your programs.
You will of course want to use many quantities which are not integers, and there are several different variable types which cover these possibilities. For real numbers there are two possibilities -- float and double. As double uses twice as much memory as float to store values, it is more accurate. For real numbers you will probably mostly want to use double. All the variable types are described below.
You can also create a String of characters. A String is like a word or a line of text and can include spaces, upper and lower case letters, numbers and other keyboard symbols. Strings are created in a similar way to variables, and the double quote symbol " is used to mark the start and end of the text. You may for example write
String name = "Bob1";This creates a String called name which stores `Bob1'.
You should be aware that there are some words which you may not use as
names for variables (or methods or classes for that matter) as they
have a special meaning in Java. These are:
abstract, boolean, break, byte, byvalue, case, cast, catch,
char, class, const, continue, default, do, double, else, extends,
false, final, finally, float, for, future, generic, goto, if,
implements, import, inner, instanceof, int, interface, long, native,
new, null, operator, outer, package, private, protected, public, rest,
return, short, static, strictfp, super, switch, synchronized, this,
throw, throws, transient, true, try, var, void, volatile, while,
widefp.
Exercise 1.3
With reference to the section above, say what type of variable would
be suitable for the following, and suggest a name for it too.
It may help to think of possible values and the range which would be valid before you choose a type.
Example: the temperature of a room in C. Room temperature is likely to vary between 15 and 35 C but it can take any value inbetween including non-integers, e.g 20.37 C, so the type must be float or double depending on the accuracy required. A suitable variable name, following the conventions described above, would be roomTemp.