Operators in Java
Definition: Operator in Java is a symbol which is used to perform operations.
We can categorize operators in two ways
Operators
Unary Binary Ternary
On the basis of number of operands
Operators
Arithematic Relational Logical Bitwise Assignment
On the basis of operations
On the basic of operands
1 Unary Operator : The Java unary operators require only one operand.
e.g. ++, --, Unary +, Unary -, Logical NOT (!) etc.
1.1 Increment (++) / Decrement (--) operators: These operators are used to Increment or
decrement the value of a variable by 1.
a++ or ++a is equivalent to a=a+1
a- - or - - a is equivalent to a=a-1
Two versions of Increment/Decrement operators:
Postfix: When a variable is followed by the Increment/Decrement operator. These operators
follow the “USE-Then-CHANGE” rule.
e.g. a++ , a- -
Prefix : When a variable is preceded by the Increment/Decrement operator. These operators
follow the “CHANGE-Then-USE” rule.
e.g. ++a , - -a
Q.1: if x=48 then find the value of y= x++ + ++x
sol. y = x++ + ++x
= 48 + 50 = 98
Initial value of x is 48 Initial value of x is 49
Postfix: first use 48 then change to 49. Prefix: first change to 50 then use 50.
final value of x is 49 final value of x is 50
2 Binary Operator: The Java Binary operators require two operands.
e.g. +,-,*,/,=,==,<,>,!= etc.
3 Ternary Operator: The Java Ternary operators require three operands. In Java, it is used as
conditional assignment operator (? :).
The general syntax of conditional operator is:
(condition) ? expression 1 : expression 2
Expression1 will assign to the variable if the condition is ‘true’ otherwise expression 2 will be
assigned.
e.g. Evaluate the value of n, if p=5 and q=19
int n =(q-p)>(p-q) ? (q-p) : (p-q) ;
sol. int n =(q-p)>(p-q) ? (q-p) : (p-q) ;
= (19-5) >(5-19) ? (19-5) :( 5-19);
= 14 > -14 ? 14 : -14
= 14 (condition results to ‘true’ hence 14 will be assigned to n)
Note: Conditional operators are equivalent to simple if-else conditional statement.
e.g. Write equivalent if-else statement for the given conditional operator.
String res = (n%2 ==0) ? ”Even” : “Odd”;
sol.
String res;
if(n%2==0)
{
res=”Even”;
}
else
{
res =”Odd”;
}
On the basis of operations
1 Arithmetic operators: These operators perform basic mathematical operations like +,-,*,/
etc.
e.g.
class OperatorExample
{
public static void main(String args[])
{
int a=10;
int b=5;
System.out.println(a+b); //results a+b = 10+5 =15
System.out.println(a-b); //results a-b = 10-5 = 5
System.out.println(a*b); //results a*b = 10 * 5 =50
System.out.println(a/b); //result a/b = 10/5 =2
System.out.println(a%b); //returns remainder of a/b i.e. 10/5 =0
}
}
2 Relational operators: These are used to compare two values and return true/false.
Operator Name Example
== Equal to x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
3 Assignment Operator ( =): it is used to assign the value of an expression to the variable of
its left side. It has following syntax:
variable = expression
e.g. int a=10; // value 10 will assign to the variable a
int sum=a+b; // sum of a and b will assign to the variable sum
Shorthand assignment operators
a += b; equivalent to a=a+b;
a -= b; equivalent to a=a-b;
a *= b; equivalent to a=a*b;
a /= b; equivalent to a=a/b;
a %= b; equivalent to a=a%b;
4: Logical Operators: Logical operators are used to determine the logic between variables or
values:
Operator Name Description Example
&& Logical Returns true if both statements are true x < 5 && x < 10
and
|| Logical or Returns true if one of the statements is x < 5 || x < 4
true
! Logical Reverse the result, returns false if the !(x < 5 && x < 10)
not result is true
Precedence and Associativity of Operators
Precedence: Precedence determines the order of operations in which an expression is
evaluated.
Associativity: If two operators have same precedence, then associativity tell the direction of
execution of operators (“Left to Right” or “Right to Left”).
The table below shows all Java operators from highest to lowest precedence, along with their
associativity.
Precedence Operator Description Associativity
[] access array element
1 . access object member left to right
() parentheses
++ unary post-increment
2 -- not associative
unary post-decrement
++ unary pre-increment
-- unary pre-decrement
+ unary plus
2 - right to left
unary minus
! unary logical NOT
~ unary bitwise NOT
(type cast) Type cast
3 new right to left
object creation
4 * / % multiplication , division , modulus left to right
+ - addition,subtraction
5 + left to right
string concatenation
<< >>
6 >>> shift left to right
< <=
7 > >= relational not associative
instanceof
==
8 != equality left to right
9 & bitwise AND left to right
10 ^ bitwise XOR left to right
11 | bitwise OR left to right
12 && logical AND left to right
13 || logical OR left to right
14 ?: ternary right to left
= += -=
*= /= %=
15 &= ^= |= assignment right to left
<<= >>= >>>=
The “new” operator: The new operator is used to allocate memory (dynamically) for the
object.
e.g. Scanner sc = new Scanner(System.in);
The dot(.) operator: An object can access instance members(variables and methods) of the
class using dot(.) operator.
Syntax for dot operator is:
<object> . <instance member>;
e.g. sc.nextInt();
Expressions: An expression is a valid combination of operators, method calls and operands
(constant or variable).
e.g. Write the Java expression for the following:
𝑥𝑦
(i) z = x3 + y3 +
2
2 𝑙
(ii) T = √
𝑔
𝑎2 +𝑏2
(iii) f =
𝑎2 − 𝑏2
Sol. (i) z = Math.pow(x,3) + Math.pow(y,3) + (x*y)/2
(ii) T =Math.sqrt(l/g)
(iii) f = (a*a +b*b) /(a*a –b*b)