Java Operators
Java Operators
OPERATORS
Operator - Definition
Operator a symbol that is used to perform mathematical
or logical operations.
Classification of operators
Binary Operators
Unary Operators
Ternary Operators
Classification of operators
An operator that performs an action on one operand is called
a unary operator (+, –, ++, – –).
An operator that performs an action on two operands is
called a binary operator (+, –, / , * , etc).
An operator that performs an action on three operands is
called a ternary operator (? :).
Binary Operators
Arithmetic Operators
Assignment Operators
Relational Operators
Logical Operators
Bitwise Operators
Shift Operators
Arithmetic Operators
Arithmetic operators are used for adding (+), subtracting (–), multiplying (*), dividing
(/), and finding the remainder (%).
Example:
a=a+5 can be written as a+=5
a=a-5 can be written as a-=5
a=a*5 can be written as a*=5
a=a/5 can be written as a/=5
a=a%5 can be written as a%=5
Relational Operators
Relational operators are used to compare two operands or
two expressions and the result is a Boolean.
Bitwise AND
int x=5, y=6,z;
z= x & y;
variable 128 64 32 8 4 2 1
x 0 0 0 0 0 1 0 1
y 0 0 0 0 0 1 1 0
x&y 0 0 0 0 0 1 0 0
Result: z=4
Bitwise OR Operator
Bitwise OR
int x=5, y=6,z;
z= x & y;
variable 128 64 32 8 4 2 1
x 0 0 0 0 0 1 0 1
y 0 0 0 0 0 1 1 0
x|y 0 0 0 0 0 1 1 1
Result: z=7
Bitwise XOR Operator
Bitwise OR
int x=5, y=6,z;
z= x ^ y;
variable 128 64 32 16 8 4 2 1
x 0 0 0 0 0 1 0 1
y 0 0 0 0 0 1 1 0
x^y 0 0 0 0 0 0 1 1
Result: z=3
Bitwise NOT Operator
Bitwise NOT operator ~, also called as bitwise complement
inverts all the bits of the operand.
For example, the number 42, which has the following bit
pattern:
var 128 64 32 16 8 4 2 1
42
128
0
640 32 116 80 4 21 1 0 1 0
213 1 1 0 1 0 1 0 1
Result: z=3
Shift Operators
These Operators are used to shift the bits of a
number left or right .
Operator Description