p03 - variables.pdf
TRANSCRIPT
-
7/31/2019 P03 - Variables.pdf
1/8
Computer Studies 2013 Syllabus
Mr. A. Gatt Page 1 of 8
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 storevalues 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.
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.
N1
N2
int int
tot
int
-
7/31/2019 P03 - Variables.pdf
2/8
Computer Studies 2013 Syllabus
Mr. A. Gatt Page 2 of 8
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.
typevariable_name;
Type must be replaced with the correct data type (seen later on) and variable_name must be set by the
user. For instance:
intN1;
intN2;
inttot;
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);
}
}
The memory locations for three
variables are created. These can
only store whole numbers.
A number is stored in the variable
using the = operator. So in this case
50 is placed in N1 and 13 in N2.
Instead of storing a particular value
in tot, the total of N1 and N2 is
stored.
-
7/31/2019 P03 - Variables.pdf
3/8
Computer Studies 2013 Syllabus
Mr. A. Gatt Page 3 of 8
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;
Then during the course of the program it is re-assigned such as:
N1 = 33;
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);
}
}
33
N1
int
50
N1
int
N1 and N2 are declared as int and
assigned a value immediately.
-
7/31/2019 P03 - Variables.pdf
4/8
Computer Studies 2013 Syllabus
Mr. A. Gatt Page 4 of 8
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 Number of Bits Used Range
byte 8 -128 to 127
short 16 -32,768 to 32,767
int 32 -2,147,483,648 to 2,147,486,647
long 64 -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 Number of Bits Used Range
float 32 1.4012984643248170710-45
to 3.402823466385288601038
double 64 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 Number of Bits Used Range
char 16 0 to 65,535
boolean 1 true or false
-
7/31/2019 P03 - Variables.pdf
5/8
Computer Studies 2013 Syllabus
Mr. A. Gatt Page 5 of 8
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 Output
char letter;
letter = C;
System.out.println(letter);
letter
char letter;
letter = C;
System.out.println(letter);
C
-
7/31/2019 P03 - Variables.pdf
6/8
Computer Studies 2013 Syllabus
Mr. A. Gatt Page 6 of 8
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 fullNameor 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 chardata type cannot be used since it
can only store one character. A Stringcan 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 Stringis to be combined with additional text, the + operator must be used, for example:
System.out.println (The next person is +fullName);
-
7/31/2019 P03 - Variables.pdf
7/8
Computer Studies 2013 Syllabus
Mr. A. Gatt Page 7 of 8
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. Explain what a variable is and why its used in a program.
2. 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. By using a Java example, show how a variable is overwritten.
4. State a suitable data type for the following values:
a. Xb. 67c. -9.45d. The clouds are gathering.e. 32000
5. Which of the following are suitable variable names? (if not state a reason why)
a. Total Sumb. TotalSUMc. totalSumd. fffe. true
-
7/31/2019 P03 - Variables.pdf
8/8
Computer Studies 2013 Syllabus
Mr. A. Gatt Page 8 of 8
6. 1 class StringExample {
2 public static void main (String args[]){
3 String firstName = John;
4 String lastName = Grech;
5 String fullName = firstName+lastName;
6 System.out.println(My name is +fullName);
7 }
8 }
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.
***