Explain about Various Types of Operators in Java?
https://www.computersprofessor.com/2016/12/explain-about-various-types-operators.html
Java has rich set of operators.
|
|
®
|
Operators are of any symbol that
tells the computer to perform certain mathematical (Or) logical
manipulations.
|
®
|
Operators are used in programs to
manipulated data is variables. They are classified as
|
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment & decrement
operators
6. Conditional operators
7. Bitwise operators
8. Special operators
|
|
Arithmetic operators :
|
|
®
|
These can operate on any built-in
numeric data type of java but can’t be operated on boolean type. They are :
+ ® Addition (Or) unary plus
– ® Subtraction (Or) unary minus
* ® Multiplication
/ ® Division
% ® Modular division
|
These operators are used as a + b, a
– b, a * b, a/b, a here a & b are known as operands.
|
|
Integer
Arithmetic: When
both the operands in expression are integers, the expression is called an
integer arithmetic expression.
If a = 14, b = 5 then
a + b = 14 + 5 = 19
a – b = 14 – 5 = 9
a * b = 14 * 5 = 70
a / b = 14 / 5 = 2 (decimal part
turreted)
a % b = 14 % 5 = 4 (remainder)
for module division, the sign of the
result is always the sign of the first operand.
–14 % 3 = – 2
–14 % – 3 = – 2
14 % – 3 = 2
|
|
Real
Arithmetic: An
arithmetic operation involving only real operands is called real arithmetic.
|
|
A real operand may assume values
either in decimal (or) exponential notation.
Ex
: x = 100 / 3.0 =
3.333
x = – 10.0 / 3.0 = – 3.333
|
|
Mixed
mode arithmetic:
When one of the operands is real is the other is integer, the expression is
called mixed mode arithmetic expression.
15 / 10.0 = 1.5
15 / 10 = 1
|
|
Relational Operators :
Comparing two qualities is depending
on their relation take certain decisions. These comparisons can be done with
the help of relation operator.
|
|
®
|
Java supports 6 relational
operators. They are :
|
< ® less
than
> ® greater
than
< = ® is less than or equal to
> = ® is greater than or equal to
= = ® is
equal to
! ® is
not equal to.
The value of relational expression
is either true (Or) false. ‘I' indicates true, ‘0’ indicates false.
Ex
: 10 < 20 = True
= 1
10 > 20 = False = 0
|
|
Logical operators :
|
|
®
|
An expression which combines two (or)
more relation expressions is termed as logical expression (or) a compound
relational expression.They are :
&& ® logica AND
!! ® logical OR
!
®
logical NOT
|
Assignment Operator : Assignment operators is used to
assign the value of an expression to a variable with the operators ‘=’.
Ex
: x = 20 ;
y = a+b;
|
|
Increment & Decrement Operators :
Java allows two
very useful operators. They are increment is decrement operators.
+ + ® Increment operator
– – ® Decrement operator
|
|
®
|
The operator + + adds ‘1’ to the operand while --
Subtracts 1.
|
®
|
Pre-increment operators ® + + m Þ m = m + 1 (or) m + = 1
|
®
|
Pre-decrement operator Þ – – m Þ m = m – 1 (or) m – = 1
Post increment operator = m + +
Post decrement operator = m – –
Ex
:
class Ss
{
public static void main (String args [ ] )
{
int a = 10, b = 20, c, d ;
d = a ++ + ++ a + ++a ;
System.out.println (“a = “+a) ;
System.out.println (“d = “ + d);
}
}
|
Conditional Operator : A ternary operator pair ?: is called
conditional operator.
Syntax : exp1 ? exp2 : exp3
Where exp1, exp2, exp3 are
expressions.
Here exp1, is evaluated first if it
is true Then exp2 is evaluated is becomes the value of the expression. If
exp1 is false, then exp3 is evaluated is becomes the value of the expression.
Ex
: a = 20;
b = 15;
x = (a > b) ? a : b;
x = (20 > 15) ? 20 : 15;
x = 20
|
|
Bitwise Operator : Java
supports bitwise operators for manipulation of data at value of bit level.
|
|
These operators are used for testing
the bits (Or) shifting there to the right (Or) left.
|
|
These operators mayn’t be applied to
float (or) double They are :
& ® Bitwise AND
|| ® Bitwise
OR
^ ® Bitwise
exclusive
~ ® One’s
complement
<< ® Left
shift
>> ® Right
shift
>>> ® Shift right with zero fill.
|
|
Special Operators : Java supports some special operators such as instance
of operator is member select on operator.
InstanceOf
Operator : This
operators allows us to determine whether the object belongs to a particular class
(or) not.
|
|
It returns true if the object on Left
hand side is an instance of a class given on Right hand side.
Ex : Person instanceof student ;
Dot Operator : The dot operator (.) is used
to access the instance variables is methods of class objects.
person1. age // Reference to the variable age
person1. Salary() // Reference
to the method salary()
|
|
It is used to access classes is
sub-packages from a package.
|