4.2 Identifiers, Data Types, Operators and Expressions 23
4.2 Identifiers, Data Types, Operators and Expressions 23
0 Java Language
Variable value 7
• <data type> refers to the type of data that will be stored in the
variable (int, float, double, boolean, char, String)
• data type refers to the type of data that will be stored in the
constant (int, float, double, boolean, char, String).
• Example of declaration:
int age;
2. float
• Used to declare variables to store floating-point
numbers (7 decimal digits).
• Example: 9.1234567
• Example of declaration:
float num;
3. double
• Used to declare variables to store larger decimal
numbers,15 significant digits such as (3.123456789)
• Example of declaration:
double Tax_Rate;
double average;
4. boolean
• A boolean value represents a true or false condition.
• The reserved words true and false are the only valid values for
a boolean type.
• Example of declaration:
boolean present;
5. char
• Used to declare variables that will store alphanumeric
data, such as ‘A’, ‘a’, ‘9’, ‘*’
• Example of declaration:
char grade;
char gender;
Checkpoint 2
Refer to Tutorial
4.2 Identifiers, data types, operators and expressions
Learning Outcomes
At the end of this topic, you should be able to:
a) Identify identifier, variable, constant and reserved word. (L)
b) Identify various primitive data types: int, float, double, boolean and char. (L)
c) Differentiate primitive data types and their usage. (T)
d) Identify various operators: arithmetic, relational and logical. (L)
e) Identify various expressions: arithmetic, relational and logical. (L)
f) Determine the operator precedence and associativity of operators. (L)
g) Convert algebraic expression into Java statement. (T)
h) Construct simple programs using primitive data types, operators and expressions.(P)
Operators
• Operators generate some computations when applied to
operands in expression.
Operator
A+B
Operand
3 Types of Operators
1. Arithmetic
2. Relational
3. Logical
1.Arithmetic Operators
• Arithmetic operations in Java includes using five arithmetic
operators to perform addition, subtraction,
multiplication, division and taking the modulus.
• Each of these operators uses two values (called operands)
to calculate a final answer.
Arithmetic Operations & Operators
Arithmetic Arithmetic Math Java
Operation Operator Expression Expression
2 * / %
3 + -
2. Relational Operators
• Relational operation refers to the comparison of values.
• Mostly used in selection and repetition control structure.
– if (mark > 40)
– if (x < y)
– if (choice == 1)
– while (counter <= 10)
– while (num != 0)
– for ( i=0; i<10; i=i+1)
Relational Operations & Operators
Relational Relational Relational
Operation Operator Expression
equal to == if (choice == 1)
less than < if (x < y)
greater than > if (mark > 40)
less than or equal <= while (c <= 10)
Variable
Steps of evaluation:
10 % 4 * 3 – 8 <= 18 + 30 / 4 - 20
2 * 3 – 8 <= 18 + 30 / 4 - 20
6 – 8 <= 18 + 30 / 4 – 20
-2 <= 18 + 30 / 4 - 20
-2 <= 18 + 7 – 20
-2 <= 25- 20
-2 <= 5 → TRUE
Evaluate this expression
Steps of evaluation:
Continue in Tutorial…
4.2 Identifiers, data types, operators and expressions
Learning Outcomes
At the end of this topic, you should be able to:
a) Identify identifier, variable, constant and reserved word. (L)
b) Identify various primitive data types: int, float, double, boolean and char. (L)
c) Differentiate primitive data types and their usage. (T)
d) Identify various operators: arithmetic, relational and logical. (L)
e) Identify various expressions: arithmetic, relational and logical. (L)
f) Determine the operator precedence and associativity of operators. (L)
g) Convert algebraic expression into Java statement. (T)
h) Construct simple programs using primitive data types, operators and expressions.(P)
Continue in practical…