You are on page 1of 26

Chapter 5: Variables and

Constants

Session Plan (Week 5):


To understand:
the concept of storage location representation
as identifiers

TMK 3102-Chap 5: Variables & 1


Constant
Identifier
 An identifier is a sequence of characters that
consist of letters, digits, underscores (_), and
dollar signs ($).
 An identifier must start with a letter, an
underscore (_), or a dollar sign ($). It cannot
start with a digit.
• An identifier cannot be a reserved word.
• An identifier cannot be true, false, or
null.
 An identifier can be of any length

TMK 3102-Chap 5: Variables & 2


Constant
Java Keywords
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 extends
char long finally strictfp volatile
class float native super while
const catch continue switch
TMK 3102-Chap 5: Variables & 3
Constant
Identifiers

Identifiers are:
 Variable
 Constant
 others

TMK 3102-Chap 5: Variables & 4


Constant
Variable
 A name associated with a memory cell whose
value can change
 Needs to be declared:
variable_type variable_name;

Example: int x;
int entry_time, charge;

TMK 3102-Chap 5: Variables & 5


Constant
Type of Variable
 Logical Type
1. Boolean
 Textual Type
2. char
 Integral Type
8 Primitives Data
3. byte Type Under 4
4. short
5. int groups
6. long
 Floating Type
7. float
8. double

TMK 3102-Chap 5: Variables & 6


Constant
Logical 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
TMK 3102-Chap 5: Variables & 7
Constant
Textual Type (char)
•Represents 16 bits unicode character
(0,65536)
•Declaration
char em ;
Declared a variable em as char type
•Must has its literals enclosed in single
quotes (‘ ‘)
em = ‘m‘
Assigning value ‘m’ to variable em
TMK 3102-Chap 5: Variables & 8
Constant
Textual Type (char)
Can be in form of :

•Escape Character
char thiIsTab;
thisIsTab=‘/t‘;

•Specific Unicode character


char phi ;
phi=‘\u03A6’; //for Φ symbol

TMK 3102-Chap 5: Variables & 9


Constant
Textual Type (char)
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

TMK 3102-Chap 5: Variables & 10


Constant
Textual Type (char)

•Has its literals enclosed in double


quotes (“ “)
•Example
greeting = “Good Morning”;
attention = “achtung!!!!”;
oneChar = “a”;

TMK 3102-Chap 5: Variables & 11


Constant
Integral 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.

TMK 3102-Chap 5: Variables & 12


Constant
Integral Type (byte , short, int,
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

TMK 3102-Chap 5: Variables & 13


Constant
FloatingType
• 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;

TMK 3102-Chap 5: Variables & 14


Constant
Variable
 Variable Declaration:

Example 2:
float matric;
matric is a ??? variable

TMK 3102-Chap 5: Variables & 15


Constant
Self Check (THINK!!!)
 What is the type of the values 0 and
‘0’?
 Determine the legalities of the following
identifiers? Greeting1
g
void
101dalmatians
Hello, World
<greeting>

 Define a variable to hold your age.


TMK 3102-Chap 5: Variables & 16
Constant
Variable

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

TMK 3102-Chap 5: Variables & 17


Constant
Variable

Example:
int quantity; Why do we
declare it as int?
float price_per_kg;
float price;
Why do we
declare them as
float?
TMK 3102-Chap 5: Variables & 18
Constant
Variable - Value Assignment

Example:
int number1, number2;

number1 = 25;
number1 25
23
?
number2 = 23;
number1 = number2; number2 23
?


TMK 3102-Chap 5: Variables & 19
Constant
Variable - Value Assignment

Example:
int quantity;
float price_per_kg, price;

quantity = 2;
price_per_kg = 4.50;
price = quantity * price_per_kg;

TMK 3102-Chap 5: Variables & 20
Constant
Variable - Value Assignment

Example:
int quantity; quantity 2?
float price_per_kg, price; price_per_kg 4.50
?
price 9.00
?
quantity = 2;
price_per_kg = 4.50;
price = quantity * price_per_kg;

TMK 3102-Chap 5: Variables & 21
Constant
Variable - Value Assignment

Example: How does this


int number1, number2; program
segment work?
number1 = 25;
number2 = 23;
number1 = number2;


TMK 3102-Chap 5: Variables & 22
Constant
Declaring and Initializing
in One Step

int x = 1;
double d = 25.4;
char b = ‘b’;
int number1 = x;

Is this statement
correct???

TMK 3102-Chap 5: Variables & 23


Constant
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.
TMK 3102-Chap 5: Variables & 24
Constant
Questions
What wrong with the Java statement
below?

byte byteVar;
int intVar = 129;
byteVar = intVar;

TMK 3102-Chap 5: Variables & 25


Constant
Thank You

TMK 3102-Chap 5: Variables & 26


Constant

You might also like