4.1 Operators Operands Expressions
4.1 Operators Operands Expressions
Skills covered:
● learn about the classifications of operators and the different operators that are present
in java and their purposes.
● learn to evaluate expressions.
● learn about implicit and explicit conversions of data types in expressions.
Observe: Hook Activity
– 14 % 3 = – 2 ● The value of relational expression is either true or
false.
– 14 % – 3 = – 2 ● * Only if the sum of values of a and b is equal to
the sum of values of c and d.
14 % – 3 = 2
– 35 >= 0 FALSE
10 < 7 + 5 TRUE
a+b==c+d TRUE
Observations
● The examples given above are known as ‘Expressions’.
● An expression always evaluates (or is processed) to a result.
● One example of an arithmetic expression is 5 + 2.
● An expression consists of operands (5 and 2 in the sample expression), operators (+
sign in the sample expression).
● The examples given earlier evaluated only to boolean, i.e. true or false values.
● Expressions with logical and or relational operators always evaluate to boolean
answers.
● Expressions with arithmetic operations always evaluate to a numeric value.
What are operators?
Java supports a rich set of operators. An operator is a symbol that tells the computer to
perform certain mathematical or logical manipulations. Operators are used in programs to
manipulate data and variables. They usually form a part of mathematical or logical
expressions.Java's operators are classified by their number of operands:
● A unary operator has one operand, for example unary minus (e.g., -5).
● A binary operator has two operands, examples are multiplication and addition.
● A ternary operator has three operands; an example is the conditional operator (?:).
A unary operator is an operator that performs its operation on only one operand. An
operator is referred to as binary if it operates on two operands
Expressions
An expression is a combination of operators, constants and variables. An expression may consist
of one or more operands, and zero or more operators to produce a value.
Operators are classified
Unary operators
● Unary operators require only
one operand.
● Examples of unary operators
include logical not, increment
operator, decrement operator etc.
● The meaning of these operators
will be dealt with in detail later in
the unit.
● Examples of expressions with
unary operation include: -a,
++5,6++,--7, 8-- etc.
How does the postfix and prefix work
Try it using both increment and decrement operators.
Decrement
Binary Operators
● Binary operators require two operands to perform the operation. Examples include all
arithmetic operators, relational operators, logical operators etc.
● Example of expressions with binary operators include: 4 * 6, 9 /3, a > b, (3 > 5) && (4 <= 0) etc
Ternary Operators
Ternary statements get their name because they take three conditions. A ternary
operator evaluates whether a statement is true or false and returns a specified
value depending on the result of the operator.
String result = (age >= 16) ? "This user is over 16." : "This user is under 16."
System.out.println(result);
}
Try to code
● Write a program to check if a number is divisible by 5 and display a suitable
output.
● Write a program to check if an integer is positive, negative or zero using the
ternary operator.
Assignment 1
Organize all the different types of operators based on the number of operators in
a Bubble Map With examples. This can be done in your notebooks.
Resources
Operators
Resource 2