Computer Applications
Grade IX
Chapter 4
Operators in Java
Operators in Java
• An operator is basically a symbol or token, which performs
arithmetical or logical operations to give meaningful
results.
Arithmetical Expression and Statement
• Arithmetical Expression: An arithmetical expression
contains variables, constants and arithmetical operators
together to produce a meaningful result.
• For Example: i) x + y ii) m – 15 iii) a*a+2*a*b+b*b
• Invalid Arithmetical expressions in java: a2+b2, 2ab
• Arithmetical Statement: When an arithmetical expression is
assigned to a variable then it is known as an “Arithmetical
statement”.
• For example: i) m = x + y ii) p = m – 15 iii) n =
a*a+2*a*b+b*b
Forms of operators
There are three forms of operators:
1) Unary operator 2) Binary operator 3) Ternary
operator
Unary operator: An arithmetical operator which is applied
to a single operand.
e.g.: unary(+), unary(-), ++, --, etc
• Unary(+) and unary(-) are used to specify the sign of the
operand. It is used before the operand.
• Example: if a = 8 then +a will be +8 and –a will be -8
• if a = -10 then +a will be -10 and –a will be +10
Unary increment and decrement operators
• Unary increment operator(++) increases the value of an
operand by one.
• Unary decrement operator(--) decreases the value of an
operand by one.
• Example:
• 1) x = x + 1 can be written as x++ or ++x
• 2) p = p – 1 can be written as p-- or --p
Prefix operators
• When increment or decrement operators are applied
before the operand, it is known as prefix operators.
• It works on the principle, “CHANGE BEFORE ACTION”
• It means the value of the variable changes before the
operation takes place.
Postfix Increment
• When increment or decrement operators are applied
after the operand, it is known as postfix operators.
• It works on the principle, “CHANGE AFTER THE ACTION”
• It means the value of the variable changes after
performing the operation.
Questions:
• 1) if p = 5, find d = ++p + 5;
• 2) if a = 48, find a = a++ + ++a;
• 3) if c = 2, find d = ++c + c++ + 4;
• 4) if m = 12, find n = m++ * 5 + --m;
• 5) if a = 4, b = 3; find the value of c = a++ * 6 + ++b*5 + 10
Shorthand operations
• An expression can be written in short form as shown in the
table
• Shorthand expression can only be used when same
variable is used before and after the assignment sign.
Examples
• If a = 8; find the value of a – = ++a + a++ + 4;
Solution: a – = ++a + a++ + 4
a = a – (++a + a++ + 4)
a = 8 – (9 + 9 + 4)
a = 8 – 22
a = –14
• If a =12, b = 8; find the value of a*= ++a/6 + b++ % 3;
Solution: a*= ++a/6 + b++ % 3
a = a * (++a/6 + b++ % 3)
a = 12 * (13/6 + 8%3)
a = 12 * (2 + 2)
a = 12 * 4
a = 48
Binary Operators
There are three different types of operators to perform
mathematical tasks:
• Arithmetical Operators: The operators which are used to
perform arithmetic calculations in a program.
• Addition(+)
• Subtraction(-)
• Multiplication(*)
• Division(/)
• Modulus(%) -> to get remainder when two numbers are
divided
• Relational Operators: These operators are used to show
relationship between the operands. Relational operators
compare the values of the variables and result in terms of
‘True’ or ‘False’
Logical Operators
• Logical Operators: These operators yield true or false
depending upon the outcome of different expressions.
The different types of logical operators along with there
format are shown below.
• Logical OR(||): Used to combine 2 or more conditional expressions.
It will result in true if any one or more expressions are true otherwise
false if none of the conditions is true.
e.g.: 5>4 || 8>12 -> true
3>7 || 5<=4 -> false
• Logical AND(&&): Used to combine 2 or more conditional
expressions. It will result in true if all the expressions are true otherwise
false.
e.g.: 5>3 && 3<5 -> true
6==6 && 3>5 -> false
• Logical NOT(!): Used when we want to reverse the result of an
expression. It is a unary operator.
e.g. !(8>3): false
!(3<0): true
Precedence of logical operators is NOT(!), AND(&&) and OR(||)
!(c<b) || (a>b) && (b>c)
Order of operation: 1 3 2
• Say for example, a = 10, b= 5 and c= 2
!(c<b) || (a>b) && (b>c)
• 1) !(c<b) is evaluated: i.e. !(2<5) -> !(true) -> false
• 2) (a>b) && (b>c) is evaluated: i.e. (10>5) && (5>2) -> true
• 3) false || true is evaluated: -> true
Examples:
Ternary Operator (Conditional Assignment)
• Deals with three operands.
• Also called conditional assignment because the value
assigned to a variable depends upon a logical expression.
• Syntax:
Variable = (test expression)? Expression 1: Expression 2;
• Variable contains the expression 1, if the test condition is
true, expression 2 otherwise
• For example a =5; b =3;
• int max = (a > b)? a: b;
‘new’ and ‘dot’ operator
‘new’
• Keyword ‘new’ is used to allocate space in the dynamic
memory for the storage of data and functions belonging
to an object in Java programming.
‘dot’ .
• The dot operator facilitates invoking members of the class
to carry out the tasks.
Hierarchy of operations
Trick to remember Hierarchy --UMARELTA
• U – Unary(plus, minus, increment, decrement, Logical Not)
• M – Multiplication/ Division/Modulus
• A – Addition/Subtraction
• R – Relational
• E – Equal To/ Not Equal To
• L – Logical
• T – Ternary
• A – Assignment, Shorthand Assignment
Exercise
1) What will be the output of following, if x =5?
i) 5* ++x ii) 5* x++
2) If a = 5, b = 9, calculate the value of : a+= a++ - ++b + a;
3) What will be the output for following program segment
int a = 0, b = 10, c = 40;
a = - - b +c ++ +b;
System.out.println("Value of a: "+a);
System.out.println("Value of b: "+b);
System.out.println("Value of c: "+c);
4) Evaluate the following expressions, if the values of the variables are:
a = 2, b = 3, and c = 9
i) a – (b++)*(--c); ii) a*(++b)%c
5) If m = 5 and n = 2, then what will be the output for following:
i) m -= n ii) n = m + m/n