0% found this document useful (0 votes)
19 views23 pages

Lecture 3 Variables 2024

Uploaded by

Lucia Motlhagodi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views23 pages

Lecture 3 Variables 2024

Uploaded by

Lucia Motlhagodi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

ISS112

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

A location in the computer’s memory where a


value can be stored for use later in a program
• Variables are commonly known as identifiers
• Must be declared with a symbolic name (label) and a type
before they can be used.
• A variable’s name enables the program to access the value
of the variable in memory.
• The name can be any valid identifier.
• A variable’s data type specifies what kind of information is
stored at that location in memory
• Once declared, it can be assigned a value
• value must have the same data type as the variable.
3
Variable Data Types

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

We will use double in our programs to represent real numbers


Examples
10.3, -0.03, 6.0
6
NOTE: Only decimal notation is allowed
char Data Type

c) char Data Type


• Used to store single characters
• We enclose each character within single
quotation marks
Data Type Value Storage (in Bytes)
char 0 to 216-1 2

Examples
’?’, ’8’, ’(’, ’X’, ’\\’, ’\n’, etc 7
boolean Data Type

d) boolean Data Type


• Used to store the two logical values:
true and false
• Used in the manipulation of logical
(Boolean) expression:
– Those that evaluate to either true or false

Data Type Value Storage (in Bytes)


boolean true or false 1 bit
8
Declaring Variables
• Syntax:
Datatype variable1, variable2 ,… ;
• Examples:
• int noOfStudents, pageNumber;
• double grossPay;
• boolean found;
• char symbol;
• Note:
• Can have one per line
• Or several per line (even more than 2)
• MUST be of the same data type 9
Declaring Variables – Memory model
noOfStudents Identifier/ label
101010 1000010

address

◼ From previous slide: (assuming variables have


values)

pageNumber 21 Value
Identifier grossPay 2435.89
found true
symbol ’%’
◼ We do not manipulate addresses directly. 10
Naming Identifiers

• Must be unique in the program


• Must start with a letter, an underscore (’_’) or $
• Can only contain letters (a - z and A – Z),
numbers(0,1, ... ,9), an underscore (’_’) and $
• Cannot be a Java keyword (reserved word)
• Refer to Java for Everyone (pg 455) for these!
• Familiarize yourself with most of them
• Java is case sensitive:
• counter is NOT the same as Counter
11
Naming Identifiers Convention

This is for good programming practice


• Variable names should start with a lowercase
letter
• E.g. number
• Each new "word“ in a variable name should begin
with an uppercase letter
• E.g. numberOfStudents, cansPerPack, totalVolume
• Variables must have meaningful names
• There are exceptions of course!
• Should be reasonably long enough
(See JFE pg 35, section 2.1.3) 12
Naming Identifiers Convention

Examples
• Good variable names
• input, myVar, numOfStudents, counter

• Illegal names —causes a syntax error


• char, ?course, 2course, course code

• Breaking Good Naming Convention


• Input, mYVar, numofstudents, X, myDog$
13
Assignment Statement

Assignment Statement: A statement used to give/assign


a value to a variable after it has been declared.
• ‘=‘ is the assignment operator
• Syntax: theVariable = expression;
• (Meaning store value of expression in theVariable)

• The value of expression MUST have the same


type as the variable. Beware!
• Examples
numOfStudents = 100;
testMark = testMark + 5; 14
Another example

(See JFE pg 34, Table 1 for more examples)


15
Initialising & Displaying Variables

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

Examples Memory diagram


int noOfStudents; noOfStudents 132
char letterGrade; letterGrade ‘B’
boolean found = true;
found true
double salary = 4676.97;
salary 4676.97
noOfStudents = 132;
letterGrade = ’B’ ;
System.out.printf( “%d\n“ ,noOfStudents);
System.out.printf( “%10s\n“ ,letterGrade);
System.out.printf( “%s\n“ , found);
System.out.printf( “%5.3f\n” , salary); 17
Initialising & Displaying Variables

More Examples Memory diagram

counter1 23 13
int counter1 = 23; counter2
23
int counter2 = counter1;

System.out.printf( “%d”, counter1);


System.out.printf(“%5d\n”, counter2);
counter1 = 13;
System.out.printf(“%d\n”, counter1);
System.out.printf(“%d\n”, counter2); 18
Programming Tips

a) Use of Javadoc comments:


• Every class should begin with a block comment
detailing (Header comment using Javadoc style
comment)
• Class name, lab session no., description, author’s
name & ID and version #
• E.g.
/**
* Class name: TopCat.java
* Lab Session: R7_295
* Description: A Java application to display a cool cat
saying hello.
* @author M. Mudongo
* @version 1.0
*/ 19
Programming Tips

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

• Reading for next lecture —JFE, Chapter 2 (operators,


arithmetic (integer & double), precedence,
expressions, mixed expressions, type conversion)

22
Computing Skills 23

You might also like