You are on page 1of 8

Computer Studies 2013 Syllabus

Variables (03)
Variables and their data types.

Introduction
In most cases, whilst a program is running, it requires memory storage for calculations or text manipulation. Most of the time, the RAM (random access memory) is used to store these temporary values; these small memory locations are called variables. When coding a program, the programmer can create variables of a particular data type which can store values throughout the execution of the program. Each variable must have a unique name so that it can be used (written to or read from). If we imagine the RAM as a set of boxes, in order to use one of these boxes, a label must be applied to it and then the data which it can store must be also declared. For instance, if we need to store three whole numbers we can create two integer (int) variables called N1, N2 and tot. N2 tot

N1

int

int

int

After these are created they can be used to store data which in this case is limited to whole numbers. Note that a variable can only store one value.

Mr. A. Gatt

Page 1 of 8

Computer Studies 2013 Syllabus

Declaring a Variable
Before using the variable it must be declared. When declaring a variable its name must be specified along with its data type in the following format. type variable_name; Type must be replaced with the correct data type (seen later on) and variable_name must be set by the user. For instance: int N1; int N2; int tot; In this case the three variables are declares as integers (int) which can only store whole numbers. There are other data types which can be used to store real numbers and text.
class VariablesExample { public static void main (String args[]){ //variables are declared int N1; int N2; int tot; //assign values to variables N1 = 50; N2 = 13; //the total of variables N1 and N2 //is stored in tot tot = N1 + N2; //Finally, we can show the result System.out.println(tot); } } Instead of storing a particular value in tot, the total of N1 and N2 is stored. A number is stored in the variable using the = operator. So in this case 50 is placed in N1 and 13 in N2. The memory locations for three variables are created. These can only store whole numbers.

Mr. A. Gatt

Page 2 of 8

Computer Studies 2013 Syllabus

Overwriting Values If a variable already stores a value and it is re-assigned a value, the previous value will be lost. For instance if N1 is assigned 50:
N1 = 50;

N1 50
int

Then during the course of the program it is re-assigned such as:


N1 = 33;

N1 33
int

The fifty is lost and replaced with 33.

Direct Initalisation Variables can also be assigned values during their declaration. So the above program can be changed to
class VariablesExample { public static void main (String args[]){ //variables are declared are assigned int N1 = 50; int N2 = 13; int tot; //the total of variables N1 and N2 //is stored in tot tot = N1 + N2; //Finally, we can show the result System.out.println(tot); } } N1 and N2 are declared as int and assigned a value immediately.

Mr. A. Gatt

Page 3 of 8

Computer Studies 2013 Syllabus

Data Types
There are eight primitive data types which are byte, short, int, long, char, float, double and
boolean.

Integers Integers allow only whole numbers. So no fractions can be stored in them. There are four types of integers. Type byte short int long Number of Bits Used 8 16 32 64 Range -128 to 127 -32,768 to 32,767 -2,147,483,648 to 2,147,486,647 -9,223,372,036,854,775,808 to 9,223,375,036,854,775,807

Variables can be declared as previously specified. So if a byte is required it can be declared as: byte month; Reals Reals can store any type of number including fractions as well. Type float double Number of Bits Used 32 64 Range 1.4012984643248170710-45 to 3.402823466385288601038 4.94065645841246544x10-324 to 1.79769313486231570x10308

Others Apart from numbers we can also store characters (a single character) and Boolean values (true or false). Type char boolean Number of Bits Used 16 1 Range 0 to 65,535 true or false

Mr. A. Gatt

Page 4 of 8

Computer Studies 2013 Syllabus

When assigning a value to a char variable, the character to be assigned must be enclosed in single quotes. For example:
class CharEx { public static void main (String args[]){ char label; label = B; } }

On the other hand a boolean variable can only store true or false; these are not to be placed in single quotes.
class BooleanEx { public static void main (String args[]){ boolean canDrive; canDrive=false; } }

Display contents of a Variable


In order to print the contents of a variable (what the variable is storing) it is important that the variable name is not placed inside the quotes otherwise the name of the variable will appear. Consider the following example.

Code
char letter; letter = C; System.out.println(letter); char letter; letter = C; System.out.println(letter);

Output

letter

Mr. A. Gatt

Page 5 of 8

Computer Studies 2013 Syllabus

Naming a Variable
When a name is given to a variable there are some rules which must be followed and some pointers which are good practice. These are some of the things to remember: it must start with a lowercase letter, it cannot be a keyword used in Java, cannot be a Boolean literal (true or false) or null, it must be unique; so throughout its scope different variables cannot have the same name, it cannot contain blank spaces; however if a variable is made up of two different words they are usually combined together using CamelCase; for example fullName or totalSum, it must reflect the contents that it will store, so that just by looking at it you know what it contains.

Strings
In most programs text needs to be stored in a program as well. The char data type cannot be used since it can only store one character. A String can store characters, numbers, a mixture of both and also symbols. Strings can be declared as the other variable types, for instance:
String fullName = Alex Gerada;

In order to display the String, the usual println() method can be used:
System.out.println(fullName);

However if the String is to be combined with additional text, the + operator must be used, for example:
System.out.println (The next person is +fullName);

Mr. A. Gatt

Page 6 of 8

Computer Studies 2013 Syllabus

Strings can also be combined together by using the + operator. For example:

class StringExample { public static void main (String args[]){ String firstName = John; String lastName = Grech; String fullName = firstName+lastName; System.out.println(My name is +fullName); } }

Activities
1. 2. Explain what a variable is and why its used in a program. Instead of first declaring a variable and then assigning it a value, direct initialisation can be used. Show the difference between these two methods by using Java examples. 3. 4. By using a Java example, show how a variable is overwritten. State a suitable data type for the following values: a. X b. 67 c. -9.45 d. The clouds are gathering. e. 32000 5. Which of the following are suitable variable names? (if not state a reason why) a. Total Sum b. TotalSUM c. totalSum d. fff e. true

Mr. A. Gatt

Page 7 of 8

Computer Studies 2013 Syllabus

6.

1 class StringExample { 2 3 4 5 6 7 8 } } public static void main (String args[]){ String firstName = John; String lastName = Grech; String fullName = firstName+lastName; System.out.println(My name is +fullName);

Copy this program and run it. Check what happens. Try to solve the problem by only changing line 5. 7. Write a program that uses three variables to store your date of birth in this format 3 February 1999. Then display the date of birth on screen using only one println() statement.

***

Mr. A. Gatt

Page 8 of 8

You might also like