0% found this document useful (0 votes)
55 views52 pages

4.2 Identifiers, Data Types, Operators and Expressions 23

The document discusses Java language topics including identifiers, data types, operators, and expressions. It covers: 1) The different types of identifiers and rules for writing valid identifiers. 2) Primitive data types in Java including int, float, double, boolean, and char and examples of declaring variables of each type. 3) The difference between variables and constants. 4) Reserved words in Java that cannot be used as identifiers. 5) The three types of operators in Java - arithmetic, relational, and logical operators.

Uploaded by

m-423224
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)
55 views52 pages

4.2 Identifiers, Data Types, Operators and Expressions 23

The document discusses Java language topics including identifiers, data types, operators, and expressions. It covers: 1) The different types of identifiers and rules for writing valid identifiers. 2) Primitive data types in Java including int, float, double, boolean, and char and examples of declaring variables of each type. 3) The difference between variables and constants. 4) Reserved words in Java that cannot be used as identifiers. 5) The three types of operators in Java - arithmetic, relational, and logical operators.

Uploaded by

m-423224
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/ 52

4.

0 Java Language

4.1 4.2 4.4 Arrays 4.5 4.6 Java


Introduction Identifiers, 4.3 Use of
Methods Programs
to Java data types, Control
Program operators Structures
and
expressions
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)
1. Identifiers
• The names that identify the element such as classes,
method and variables in a program.
• A series of characters consisting of letters, digits
underscore (_) and dollar sign ($).
• Should be unique and meaningful.
• Case-sensitive – uppercase and lowercase letter is
considered different (num, Num, NUM).
Rules to write valid identifiers
• First character must be a letter or an underscore or a dollar
sign, cannot begin with a digit.
• Cannot use reserved words, such as if, while, int, do, float,
true, false, or null.

• Examples of valid identifiers:


Number, num1, x2, $total, Total_Pay
Examples of Identifiers
Valid Identifiers Invalid Identifiers
• width • Student age
• circle_area • Sale-price
• MassInKilogram • Sphere.volume
• _pngk • Total+hour
• $BR1M • 007bond
• Student2 • case
Are these identifiers valid or invalid?
If invalid state the reason
• N0_of_kilo
• Myfirst.c
• _abc
• 3X
• %change$
• $123
• $Data-1
• while
Are these identifiers valid or invalid?
If invalid state the reason
• N0_of_kilo • valid
• Myfirst.c • Invalid – use (.)
• _abc • valid
• 3X • Invalid – start with number
• %change$ • Invalid – start and use symbol %
• &123 • valid
• $Data-1 • Invalid – use (-)
• while • invalid – use reserved word
2. Variables
• Variable is a location in the computer’s memory to store a
value.
• A variable must be declared with a name and a data type
before it can be used.
Variable
name age

Variable value 7

• The value of a variable can be changed during program


execution.
Variable declaration
• The general form of a variable declaration is:
<data type>space<variable name>;

• <data type> refers to the type of data that will be stored in the
variable (int, float, double, boolean, char, String)

• <variable name> can be any valid identifier.

• Examples of variable declaration:


int radius;
float circle_area;
char grade;
Constant
• A constant is an identifier that is similar to a variable
except that it holds fixed value while the program is
active.

• The compiler will issue a run-time error if you try to


change the value of a constant during execution.
Constant name PI
Constant value 3.142
Constant
• A final variable is a constant. It is an identifier with a
value that cannot be changed during normal execution.

• A constant must be declared with a name and a data


type.

• Use all-uppercase names for program readability.


Constant declaration
• The general form of a constant declaration is:
final data type CONSTANT_NAME = value;

• data type refers to the type of data that will be stored in the
constant (int, float, double, boolean, char, String).

Examples of constant declaration

final int PASS_MARK = 40;


final double PI = 3.142;
3. Reserved Word
• Java reserved words are keywords that are reserved by Java
functions.
• It cannot be used as identifiers (e.g., variable names, method
names, class names).
• It identify language entities, such as data type, input/output,
and others.
• Have special meanings to the compiler.
• Examples:
– class, public, static, void, return, int, double, boolean, true,
false …
List of reserved word
abstract assert boolean break byte case

catch char class const continue default

double do else enum extends false

final finally float for goto if

implements import instanceof int interface long

native new null package private protected

public return short static strictfp super

switch synchronized this throw throws transient

true try void volatile while


Checkpoint 1

1. Declare a variable name “average” with double data type.


2. Declare a constant name “PASS MARK” with integer data type.
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)
Primitive Data Types
• Primitive data types are the most basic data types
available within the Java language.

• Five (5) primitive data types:


1. int (for integer)
2. float (for floating-point)
3. double – (for floating-point or decimal number)
4. boolean – (for true/ false value)
5. char – (for character)
1. int
• Used to declare variables to store whole numbers
(7, -11, 0, 319)
• ( Example: age, year, number of children)

• 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.

• A boolean can also be used to represent any two states, such


as a light bulb being on or off.

• 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’, ‘*’

• char variable may hold only one character (i.e. a single


lowercase or uppercase letter, a single digit or a single
special character).

• Example of declaration:
char grade;
char gender;
Checkpoint 2

1. List 5 primitive data type.


2. Explain the use of each data type
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)
Differentiate primitive data types and
their usage
primitive data type usage

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

Addition + a+b a+b


Subtraction - a-b a–b
Multiplication * axb a*b
Division / a / b or a ÷ b a/b
Modulus
(Remainder)
% a mod b a%b
Precedence of Arithmetic Operators
Arithmetic
Precedence Level
Operators
1 ()

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)

greater than or equal >= if (age >= 40)

Not equal != while (num != 0)


3. Logical Operators
• Logical operation is applied to one or more relational
expression.
• It is used to:
- connect two or more relational expressions
- negate (inverse) a relational expression
Logical Operations & Operators
Logical Logical
Java Expression
Operation Operator

AND && if (age > 25) && (grade ==‘A’)

OR || if (age > 25) || (grade ==‘A’)

NOT ! if !(age > 25)


|| - pipe, ! – exclamation mark , & - ampersand
Assignment Operators
• Used to assign (give) a value or results of an
expression on the right-side to a variable on the
left-side of an equation.
• Assignment operator is “ = “
• Examples:
x=1
y=3
total = x + y
Assignment Operation
• Examples:
x = 1
y = 3
total = x + y

Variable

▪ Variable x gets the value 1


▪ Variable y gets the value 3
▪ Variable total gets the value 4
Checkpoint 3

Identify the types of operators

Java Expression Types of Operators


if (age > 25) || (grade ==‘A’)
while (c <= 10)
c=a%b
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)
Expressions
• Expression in Java are formed by properly combining
operators, variable and constants.
• Java expression can be complex.
• Parentheses ( ), can be used to force order of
evaluation.
• Expression may also contain spaces for readability.
Arithmetic Expressions
• Total = x + y + z;
• Net_pay = gross_pay – deduction;
• WAGE = (basic_pay + overtime * rate) – (kwsp + loan);
• c = b % a;

Result is a numeric value ( 12, 124, 3.234)


Relational Expressions
• if (a == b)
• if (b * b – 4 * a * c) > 0
•a>b

Result in boolean value ( true, false)


Logical Expressions
• !false
• true && false
• (gender ==‘m’) && (age > 20)
• true || false
• (gender ==‘m’) || (status == 1 ) && (age >= 21)

Result in boolean value ( true, false)


Checkpoint 4

Identify the types of expression

Java Expression Types of Expression


true && false
Total = x + y + z;
a>b
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)
Precedence of Operators
OPERATOR DESCRIPTION ASSOCIATIVITY
HIGHEST () Grouping Evaluate from inside out
! NOT Right to Left
* / % Multiplication, Left to right
division, remainder
+ - Addition, subtraction Left to right
< <= > >= == != Relational Left to right
&& AND Left to right
|| OR Left to right
LOWEST = Assignment Right to Left
Evaluate this expression
1. 10 % 4 * 3 – 8 <= 18 + 30 / 4 – 20

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

2. ( 4.2 >= 5.0) && (8 == (3 + 5))

Steps of evaluation:

(4.2 >= 5.0) && (8 == (3 + 5))


False && (8 == (3 + 5))
False && (8 == 8)
False && True → False
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)
Convert Algebraic expression into Java statement

Algebraic expression Java statement


length x width = area area=length*width;
a+b/c=d d=a+(b/c);
y=ab y=Math.pow(a,b);

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…

You might also like