8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 5
1/26
TMK 3102-Chap 5: Variables &
Constant
1
Chapt er 5: Var iables andConstants
Session Plan (Week 5):To understand:the concept of storage location representationas identifiers
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 5
2/26
TMK 3102-Chap 5: Variables &Constant
2
Ident i f ie r An identifier is a sequence of characters that
consist of letters, digits, underscores (_), anddollar signs ($).
An identifier must start with a letter, anunderscore (_), or a dollar sign ($). It cannotstart with a digit. An identifier cannot be a reserved word.
An identifier cannot be true, false, ornull. An identifier can be of any length
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 5
3/26
TMK 3102-Chap 5: Variables &Constant
3
J ava Keyw ords
do new abstract break synchronized
if default assert private implements
int goto this throw protected
try return import public instanceof
byte else throws double transient
case void boolean short package
for final interface static extendschar long finally strictfp volatile
class float native super while
const catch continue switch
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 5
4/26
TMK 3102-Chap 5: Variables &Constant
4
Ident i f ie rs
Identifiers are:
Variable Constant
others
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 5
5/26
TMK 3102-Chap 5: Variables &Constant
5
Var iab le
A name associated with a memory cell whosevalue can change
Needs to be declared:variable_type variable_name;
Example:int x;int entry_time, charge;
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 5
6/26
TMK 3102-Chap 5: Variables &Constant
6
Type of Var iab le
Logical Type
1. Boolean
Textual Type
2. char
Integral Type
3. byte
4. short
5. int
6. long
Floating Type
7. float
8. double
8 Primitives DataType Under 4
groups
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 5
7/26
TMK 3102-Chap 5: Variables &Constant
7
Logic a l Type (boolean)
boolean data type has 2 literals
true and false
example of declarations
boolean status;
Declared a variable status as a
boolean type.status = true;
assign a value of true to variable status
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 5
8/26
TMK 3102-Chap 5: Variables &Constant
8
Tex t ua l Type (c har )
Represents 16 bits unicode character
(0,65536)
Declarationchar em ;
Declared a variable em as char type
Must has its literals enclosed in single
quotes ( )
em = m
Assigning value m to variable em
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 5
9/26
TMK 3102-Chap 5: Variables &Constant
9
Tex t ua l Type (c har )
Can be in form of :
Escape Character
char thiIsTab;
thisIsTab=/t;
Specific Unicode character
char phi ;
phi=\u03A6; //for symbol
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 5
10/26
TMK 3102-Chap 5: Variables &Constant
10
Tex t ua l Type (c har )
How do we presenting words or sentences?
use String data type.
it is not part of primitive data type. It
is a class.
example of declarations
String greeting;
Declared a variable greeting as a string type.
String attention, oneChar;//another
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 5
11/26
TMK 3102-Chap 5: Variables &Constant
11
Tex t ua l Type (c har )
Has its literals enclosed in double
quotes ( )
Example
greeting = Good Morning;
attention = achtung!!!!;
oneChar = a;
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 5
12/26
TMK 3102-Chap 5: Variables &Constant
12
In t egra l Type
byte 8 bit (-128,127)
short 16 bit (-32768,32767)
int 32 bit (-2147483648,2147483647)
long 64 bit (-922337203684547758808
,92233720368454775880
example of declarations
int markah;
declared a variable markah
markah = 76;
assign a value 76 to it.
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 5
13/26
TMK 3102-Chap 5: Variables &Constant
13
In t egra l Type (byt e , shor t , in t ,long)
Has 3 forms: Decimal, Octal and
Hexadecimal
int jumlah;jumlah = 77; //decimal
jumlah = 080;//octal
//leading 0 indicate octal value
jumlah = 0xBAAC; //hexa
//leading 0x indicates hexadecimal value
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 5
14/26
TMK 3102-Chap 5: Variables &Constant
14
Float ingType
float 32 bit (1.4e-045,3.4e+038) precision = 6,7
double 64 bit (4.9e-324,1.8e+308) precision=15
example of declarations
float CGPA;
double diameter;
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 5
15/26
TMK 3102-Chap 5: Variables &Constant
15
Var iab le
Variable Declaration:
Example 2:
float matric;
matric is a ??? variable
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 5
16/26
TMK 3102-Chap 5: Variables &Constant
16
Self Chec k (THINK !!!)
What is the type of the values 0 and0?
Determine the legalities of the followingidentifiers?
Define a variable to hold your age.
Greeting1
g
void
101dalmatians
Hello, World
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 5
17/26
TMK 3102-Chap 5: Variables &Constant
17
Var iab le
Example:
Calculate and display the price of a number of apples if
the quantity in kg and price per kg are given.
Input: quantity and price_per_kg
Output: price
Process: price = quantity * price_per_kg
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 5
18/26
TMK 3102-Chap 5: Variables &Constant
18
Var iab le
Example:
int quantity;
float price_per_kg;
float price;
Why do wedeclare it as int?
Why do we
declare them as
float?
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 5
19/26
TMK 3102-Chap 5: Variables &Constant
19
Variable - Va lue Assignm ent
Example:
int number1, number2;
number1 = 25;
number2 = 23;
number1 = number2;
number1 ?
number2 ?
2523
23
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 5
20/26
TMK 3102-Chap 5: Variables &Constant
20
Variable - Va lue Assignm ent
Example:
int quantity;
float price_per_kg, price;
quantity = 2;
price_per_kg = 4.50;price = quantity * price_per_kg;
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 5
21/26
TMK 3102-Chap 5: Variables &Constant
21
Variable - Va lue Assignm ent
Example:
int quantity;
float price_per_kg, price;
quantity = 2;
price_per_kg = 4.50;price = quantity * price_per_kg;
quantity ?
price_per_kg ?
price ?
4.50
9.00
2
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 5
22/26
TMK 3102-Chap 5: Variables &Constant
22
Variable - Va lue Assignm ent
Example:
int number1, number2;
number1 = 25;
number2 = 23;
number1 = number2;
How does this
program
segment work?
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 5
23/26
TMK 3102-Chap 5: Variables &Constant
23
Dec lar ing and In i t ia l izingin One St ep
int x = 1;
double d = 25.4;
char b = b;int number1 = x;
Is this statementcorrect???
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 5
24/26
TMK 3102-Chap 5: Variables &Constant
24
Constant
A variables whose values does not change.
How??
Put keyword final in front of the variable.
Do it in one step (declared and initialized)
final float PI = 3.14278;
final int dayOfJanuari = 31.
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 5
25/26
TMK 3102-Chap 5: Variables &Constant
25
Quest ions
What wrong with the Java statementbelow?
byte byteVar;
int intVar = 129;
byteVar = intVar;
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 5
26/26
TMK 3102-Chap 5: Variables & 26
Thank You