Java Operators, Ch-2 (Class - Ix, 2023)
Java Operators, Ch-2 (Class - Ix, 2023)
Definition: An operator is a special symbol used to perform various mathematical and logical
operations on operands. Operators are used in programs to manipulate data and
variables/constants.
(In short, we can say operators are symbolic instructions used to perform mathematical
and logical operations on operands.)
Operands are data values involved in the operation (They can be variables or constants)
The valid combination of both operands and operators make an expression.
expression
1) Arithmetic operators:
These operators are used in Java programs to perform arithmetic / mathematical
calculations.
These operators are used in mathematical expressions in the same way that they are used in algebra.
e.g. * , / , % , + , - ( a+b , a - b , a / b , a * b , a % b)
Special Note 1:/ This operator is used to calculate quotient.
Note 2: % This operator is used to calculate remainder.
Note 3: In Java an integer divided by an integer must be an integer.
e.g. 9/2= 4 , But 9.0/2= 4.5
e.g. 9%4 =1 (here reminder is calculated)
2) Relational operator:
Relational operators are used to compare values of the operands. These operators return a Boolean value
(either true or false) based on the state of the operands.
Note: The expression which is composed of two or more relational expressions is known as logical expression or
compound relational expression.
1
e.g. Logical AND && ( “&” is known as ampersand )
OR ( || ) : Logical OR returns true, if one or more specified relational expression(s) is/are evaluated
to true, otherwise it returns false.
Expression(E1) Result(!E1)
T F
F T
e.g. ! (10<20) ------ Ans : false [ because (10<20 ) is true]
2
4) Assignment operator( = ) (equal to sign)
This operator is used to assign value to a variable/constant. It assigns the value on its right side to the
variable written to the left of it.
e.g. a) Mks=100; [by this 100 is stored in Mks]
b) Tot =m+c+e; [sum of m, c and e is assigned to Tot]
5) Increment and Decrement operators (Very Important)
Increment(++) / Decrement(--) operator is used to increase/decrease value of the specified operand by
one(1) respectively.(example of Unary operator)
By using these operators, we can write the expressions: x=x+1 as x++ or ++x and x=x-1 as x-- or --x.
Example 1) Example 2)
int x=10; int x=10;
int y=++x ; Ans) x=11 and y=11 int y=x++ ; Ans) x=11 and y=10
UNARY MINUS(-) : The operator unary minus(-) also precedes an operand. This operator inverts
the value of an operand. (e.g., if a=20 ; then -a means -20)
Binary operators demand two operands to perform action. ( e.g. +,-, * ,> , < ,= , == etc.)
Examples of expression with binary operator: a + b , a> b and a!=c
3
7) Ternary/conditional operators: (Very Important)
This operator requires three operands and it is used to store a value in a variable depending upon a condition(s).
It is a replacement of if-else statement of Java. It is also called conditional assignment operator.
Syntax:
[Take this value if condition is false]
e.g., int gt =( 20 >=10 ? 20 : 10); Ans) gt=20 [Because the Test Expression(20>=10) is true]
int res =( 5 !=5 ? 5*5 : 5*5*5); Ans) res=125 [Because the Test Expression(5!=5) is false]
4
VERY IMPORTANT NOTES:
Types of Expressions:
Arithmetic Expression: It is a valid combination of operands and only arithmetic operators.
e.g. a+b , a+b*c , (x+y) * (x-y)
Logical Expression: It is a valid combination of two or more relational expressions and only
logical operators.
e.g. (a>=b && b>=c) , (45>=34 || 10 !=10) , !(x==y)
Operator precedence determines the order in which different operators in an expression are evaluated.
Note : Precedence can be changed by placing parentheses around the expressions that need to be
evaluated first.
Operator associativity determines the order of operation when an expression contains operators of same
precedence. (to evaluate/solve the expression from left to right OR from right to left)
**********