Operators in java programming language:
Operators
are the symbols which are use for perform operation on operant(value).
·
Types of operators based on the number of
operant
I.
Unary operator(Single operant)
II.
Binary operators(Double operant)
III.
Ternary operators(Three operant)
·
Types of operators based on task
I.
Arithmetic operator
II.
Assignment operator
III.
Relational operator
IV.
Conditional operator
V.
Increament & Decrement operator
VI.
Logical operator
VII.
Miscellaneous operator
I. Arithmetic operators:
Operators
used to perform basic arithmetic operations
Operation |
Operant
1 |
Operator |
Operant
2 |
Result |
Type |
Addition |
10 int |
+ |
20 int |
30 |
int |
Subtraction |
‘a’ char |
- |
‘b’ char |
-1 |
int |
Multiplication |
0.03 f |
* |
0.02 f |
5.99999 |
float |
Division |
30 int |
/ |
10 int |
3 |
int |
Modulus |
30 int |
% |
10 int |
0 |
int |
Note: when we perform
arithmetic operation using Boolean and string operant, we get error.
II. Assignment operators:
It is a binary operator
use to store value in variable. Assignment operator works right to left.
Syntax:
Datatype
variablename = value/expression ;
Rule: The
value must be in RHS & the variable name should be in LHS.
Step 1: If
we have an expression at RHS. first expression is solved then value is given to
the LHS
Example: int b = 10+20;
Step 2: If
the value in RHS is not same as the type of container LHS.
·
Compiler try to do widening if the conversion is
successful, we don’t get any compile time error
·
The widening is not possible we get compile time
error(incompatible type).
III. Relational operator:
Relational
operator helps us to compare two values for decision making. Relational operators are binary operators.
Return type of relational operator is Boolean type (True & False).
Operator |
Operant
1 |
Symbol |
Operant
2 |
Result |
Explanation |
Equality operator |
10 |
== |
20 |
False |
If operant 1 & operant 2 are equal we get true if not get false |
Less than operator |
10 |
< |
20 |
True |
If operant 1 is less than operant 2 we get
true if not get false |
Greater than operator |
10 |
> |
20 |
False |
operant 1 is
greater than operant 2 we get true if not get false |
Less than equal to
operator |
10 |
<= |
20 |
True |
operant 1 is less than or equal to operant 2 we get true if not get false |
Greater than equal to operator |
10 |
>= |
20 |
False |
operant 1 is greater than or equal to operant 2 we
get true if not get false |
Not equal to operator |
10 |
!= |
20 |
True |
If operant 1 & operant 2 are not equal we get true if not get false |
IV. Conditional operator:
Conditional operator is ternary operator(
it accept 3 operant). Conditional operators are used for decision making
Syntax:
Literal/expression/variable ?
Literal/Expression/Variable : Literal/Expression/Variable ;
·
Operant 1 must be a condition
·
Condition must return Boolean type value
·
If the condition returns true control go to
operant 2
·
If the condition returns false control go to operant 3
·
Result type of conditional operator depends on
the type of operant 2 & operant 3
Example:
Class Cond1
{
Public Static void main(String []
args){
Int a = 33;
Int b =66;
String result = a>b ? “ A is
greater” : “B is greater”;
System.outprintln(result); // “B
is greater”
}
}
V. Increment & Decrement operator
1.
Increment operator:
·
Pre-increment:
It is a unary operator. If a variable is prefixed with increment operator,
then it is known as pre-increment operator.
Syntax:
++ variable ;
Example:
Class PreInc
{
Public static void
main(String [] args)
{
Int i = 100;
System.out.println(i);
// 100
System.out.println(++i);
// 101
}
}
·
Post-increment:
It is a unary operator. If a variable is suffixed with increment operator,
then it is known as post-increment operator.
Syntax:
Variable ++;
Example:
Class PostInc
{
Public static void
main(String [] args)
{
Int i = 100;
System.out.println(i);
// 100
System.out.println(i++);
// 100
System.out.println(i++);
// 101
}
}
2.
Decrement operator:
·
Pre-decrement:
It is a unary operator. If a variable is prefixed with decrement operator,
then it is known as pre-decrement operator.
Syntax:
--variable ;
Example:
Class PreDec
{
Public static void
main(String [] args)
{
Int i = 100;
System.out.println(i);
// 100
System.out.println(--i);
// 99
}
}
·
Post-decrement:
It is a unary operator. If a variable is suffixed with decrement operator,
then it is known as post-decrement operator.
Syntax:
Variable --;
Example:
Class PostDec
{
Public static void
main(String [] args)
{
Int i = 100;
System.out.println(i);
// 100
System.out.println(i--);
// 100
System.out.println(i);
// 99
}
}
VI. Logical operators:
Logical operator used to make
logical decisions. Logical operators work similar to logical gates.
·
Types of logical operators:
1)
AND operator(&&)
2)
OR operator(||)
3)
NOT operator(!)
4)
XOR operator(^)
1.
AND operator(&&)
It’s a binary operator.
Operant1 |
Operant2 |
Result |
True |
False |
False |
False |
True |
False |
False |
False |
False |
True |
True |
True |
2.
OR operator(||)
It is a binary operator.
Operant1 |
Operant2 |
Result |
True |
False |
True |
False |
True |
True |
False |
False |
False |
True |
True |
True |
3.
NOT operator(!)
It is unary operator
Operant1 |
Result |
True |
False |
False |
True |
4.
XOR operator
It is binary operator
Operant1 |
Operant2 |
Result |
True |
False |
True |
False |
True |
True |
False |
False |
False |
True |
True |
False |
Note:
·
Type of operant for logical operator must be of Boolean
return type.
·
Result of logical expression is Boolean type
Example:
Class LogicalOp
{
Public static void
main(String [] args)
{
System.out.println(true
&& false); // False
System.out.println(‘a’>’b’
|| ‘z’<’o’); // False
System.out.println(true
|| false); // True
System.out.println( !false);
// True
}
}
VII. Miscellaneous operators
Operator |
Description |
Example |
?: |
Ternary (conditional)
operator |
a > b ? a : b |
instanceof |
Type check operator |
obj instanceof String |
. |
Member access |
obj.method() |
[] |
Array element access |
arr[0] |
(type) |
Type casting |
(int) 10.5 |
new |
Object/array creation |
new String() |
this |
Refers to current
object |
this.var |
super |
Refers to superclass
methods/variables |
super.method() |