0% found this document useful (0 votes)
11 views

Java Operators, Ch-2 (Class - Ix, 2023)

Uploaded by

lahiri8919
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Java Operators, Ch-2 (Class - Ix, 2023)

Uploaded by

lahiri8919
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Computer, Class-IX(FILE-2) 2023

Java Operators (Ch-2)(for ICSE Board Exam.)

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.

e.g. a + b ( here a & b are operands and + is an operator)

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.

e.g. > , < , = = , ! = , > = , < =


e.g. 50>=50 Ans : true
100 != 100 Ans: false
100 = = 150 Ans: false
40 <= 40 Ans: true
3) Logical operators:
These operators are used to combine conditional expressions to set complex condition(s). It returns a Boolean
value(either true or false).

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 )

Logical OR  || ( “|” is known as pipe )

Logical NOT  ! ( “!” is known as exclamatory mark)

Truth tables of Logical operators:


AND (&&) : Logical AND returns true, if all the specified relational expressions are evaluated to
true, else it returns false.

Expression1 Expression2 Result


(E1) (E2) (E1 && E2)
F F F
F T F
T F F
T T T

e.g. (34<=56 && 45==45 ) Ans—true


(30<10 && 45>=40 ) Ans—false
(30<10 && 400<200) Ans—false

OR ( || ) : Logical OR returns true, if one or more specified relational expression(s) is/are evaluated
to true, otherwise it returns false.

Expression1 Expression2 Result


. (E1) (E2) (E1 || E2)
F F F
F T T
T F T
T T T

e.g. (34<=56 || 45==45 ) Ans—true


(30<10 || 45>=40 ) Ans—true
(30<10 || 400<200) Ans—false
NOT ( ! ) : Logical NOT is an example of Unary operator, which deals with only one operand(relational
expression)
The Logical NOT(!) operator reverts (negates or reverses) the result of the given expression.

Expression(E1) Result(!E1)
T F
F T
e.g. ! (10<20) ------ Ans : false [ because (10<20 ) is true]

! (100<=20) ---- Ans : true [ because (100<=20 ) is false]

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.

* Prefix Increment/ Decrement (Change before action)


* Postfix Increment/ Decrement (Change after action)
Prefix Increment (++x) : It increases the value of operand by 1, and then evaluates the expression.
Prefix Decrement (--x) : It decreases the value of operand by 1, and then evaluates the expression.
Postfix Increment (x++): It evaluates the expression, and then increases the value of operand by 1.
Postfix Decrement (x--) : It evaluates the expression, and then decreases the value of operand by 1.

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

6) Unary and binary operators:


 Unary operators demand only one operand to perform action. ( e.g. ++ , -- , ! )
Note)
UNARY PLUS(+) : The operator unary plus(+) can be used before the operand. This operator results
in the same value of the variable. (e.g., if a=20 ; then +a means 20)
 The value without + operator is also treated as positive value by default.

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]

Variable= Expression1 ? Expression2 : Expression3

[Condition(s) to be checked] [Take this value if condition is true]

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]

8) Compound assignment operator: (Very special)


These operators are used to perform arithmetic and assignment operations on the specified operand(s)
simultaneously. (Also known as Java shorthand)
e.g. += , -= , *= , %= , /=

9) Special operators (Miscellaneous Operators)


Java provides few special operators to perform some useful operations. e.g. new operator, Dot(.) operator.
 new operator is used to create a class object or an array. It allocates memory and returns the reference
of memory at run time.
 Dot(.) operator is used to access members of a class with the help of an object or class name.
e.g., Math.pow(5) , Math.sqrt(36)

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)

Relational Expression: It is a valid combination of operands and only relational operators.


e.g. a>b , a<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 & Associativity : (* Very Special )

 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)

Operators Precedence Table


ORDER OPERATORS
1 ( ), [ ] Highest
2 ++ , - - , !
3 *, / , %
4 +, -
5 > , >=, < ,<=
6 = = , !=
7 &&
8 ||
9 ?:
10 =, += ,-= ,*= , /= , %= Lowest

**********

You might also like