Lecture 3 Variables 2024
Lecture 3 Variables 2024
Lecture 3 Variables 2024
Introduction to programming
Lecture 3: Variables
Outline
LJECTIVES
• By the end of this lecture you must be able to:
– Know variables
– Give a variable an appropriate name
– Identify an appropriate data type for a value
– Variable declaration
– Assign it a value of the same type
– Draw Memory Model diagrams (primitives)
– Print value of a variable to the screen
– Programming Tips
2
Variables
Data type:
A set of values together with a set of operations
• Example
• set of numbers + , × , - , ÷, etc
• Categories of data types
• Primitive data type
• Reference data type
Primitive Data type:
A data type built into the language.
Also called fundamental data type.
Does not require any special operations to be used.
4
int Data Type
a) int Data Type
• Used to store integers
Data Type Values Storage (in bytes)
byte -28 to 27 1
short -215 to 215-1 2
int -231 to 231 -1 4
long -263 to 263-1 8
Examples
2, 20, -200, 12345, 123456789234
NOTE: No fractions 5
Floating-point Data Type
b) Floating-point Data Type
• Used to store floating-point values, also known as
real numbers
Data Type Value Storage
(in Bytes)
float -3.4E + 38 to 3.4E + 38 4
double -1.7E + 308 to 1.7E + 308 8
Examples
’?’, ’8’, ’(’, ’X’, ’\\’, ’\n’, etc 7
boolean Data Type
address
pageNumber 21 Value
Identifier grossPay 2435.89
found true
symbol ’%’
◼ We do not manipulate addresses directly. 10
Naming Identifiers
Examples
• Good variable names
• input, myVar, numOfStudents, counter
a) Initializing Variables
Memory diagram
2 possible ways
• Declare the variable first:
quit ‘Q’
char quit;
and then assign value:
quit =’Q’;
• Declare and initialize in one step:
char quit =’Q’;
b) Displaying value of a variable
• Syntax:
System.out.printf(“%...”, varName);
• This will send the contents of variable varName to the
screen ( using specified format)
• See JFE pgs 50 & 51 for formats to apply 16
Initialising & Displaying Variables
counter1 23 13
int counter1 = 23; counter2
23
int counter2 = counter1;
b) Indentation:
• Indent code within a block, this include aligning the
opening and closing braces.
• Example of indented code
public class HelloPrinter
{
public static void main (String[] args)
{
System.out.println("Hello world!");
}
}
NOTE: These two make the program more readable and
understandable. These count towards grading! 20
Exercises
a) Pick legal Java variable names from the following:
- goto, Mum, 1stmum, mymum, _mum, new, $mum, my
mum, strictfp, private_protected, no, try$this, two, illegal, 1,
finally.
b) Identify a more suitable data type for each of the following
- Your monthly allowance, your first name’s initial and your
friend’s age
c) Identify illegal assignment statements & justify why.
int j = true; one = 7; 6 = k; boolean stop = yes;
boolean flag = False; int age = 5.5;
21
Summary
- Variables
- Variable declaration
- Assignment statement
- Memory Model diagrams (primitives)
- Printing variables
- Programming Tips
22
Computing Skills 23