|
Operators are used to manipulate primitive data types. In Java, there are six categories of operators.
Assignment Operator:
Assignment operator is the most common operator almost used with all programming languages. It is represented by "=" symbol in Java which is used to assign a value to a variable lying to the left side of the assignment operator.
x=y+z;
Arithmetic Operators:
Arithmetic Operators are used to perform some mathematical operations like addition, subtraction, multiplication, division, and etc.
Symbol
|
Operator Name
|
Example
|
+
|
Additive Operator
|
n = n + 1;
|
-
|
Subtraction Operator
|
n = n - 1;
|
*
|
Multiplication Operator
|
n = n * 1;
|
/
|
Division Operator
|
n = n / 1;
|
%
|
Remainder Operator
|
n = n % 1;
|
Unary Operators:
The unary operators require only one operand to perform different kind of operations such as increasing/decreasing, negating an expression, or inverting a boolean value.
Symbol
|
Operator Name
|
Operation
|
+
|
Unary plus operator
|
indicates positive value (however, numbers are positive without this)
|
-
|
Unary minus operator
|
negates an expression
|
++
|
Increment operator
|
increments a value by 1
|
--
|
Decrement operator
|
decrements a value by 1
|
!
|
Logical compliment operator
|
inverts a boolean value
|
Equality and Relational Operators:
Equality and relational operators are used to compare the results of two expressions or operands in a program to know whether an operand is equal, not equal, greater than, less than to another operand.
Symbol
|
Operator Name
|
Example
|
= =
|
Equal to
|
a = = b
|
!=
|
Not equal to
|
a ! = b
|
>
|
Greater than
|
a > b
|
<
|
Less than
|
a < b
|
>=
|
Greater than or equal to
|
a > = b
|
<=
|
Less than or equal to
|
a > = b
|
Conditional or Logical Operators:
Conditional operators return a true or a false value based on the state of the variables i.e. the operations using conditional operators are performed between the two boolean expressions.
Symbol
|
Operator Name
|
&
|
AND
|
&&
|
Conditional-AND
|
|
|
OR
|
||
|
Conditional-OR
|
!
|
NOT
|
? :
|
Ternary (shorthand for if-then-else statement)
|
Bitwise and Bit Shift Operators:
The bitwise and bit shift operators are used to manipulate the contents of variables at a bit level according to binary format. These operators perform bitwise and bit shift operations on integral type variables.
Symbol
|
Operator Name
|
Example
|
~
|
Unary bitwise complement
|
~op2
|
&
|
Bitwise AND
|
op1 & op2
|
|
|
Bitwise inclusive OR
|
op1 | op2
|
^
|
Bitwise exclusive OR
|
op1 ^ op2
|
<<
|
Signed left shift
|
op1 << op2
|
>>
|
Signed right sift
|
op1 >> op2
|
>>>
|
Unsigned right shift
|
op1 >>> op2
|
Type Operators :
instanceof is a run-time operator used to compare a class and an instance of that class. This operator " instanceof" compares an object to a specified class type.
The instanceof operator is defined to know about an object's relationship with a class. It evaluates to true, if the object or array is an instance of the specified type; otherwise it returns false.
Operator Precedence
When two operators share an operand then operator with the higher precedence gets evaluated first. However, if the operators have the equal precedence in the same expression then that expression will be evaluated from left to right except the assignment operators.
Precedence
|
Operator
|
Type
|
Associativity
|
|
15
|
()
[]
·
|
Parentheses
Array Subscript
Member selection
|
Left to Right
|
|
14
|
++
--
|
Unary post-increment
Unary post-decrement
|
Right to left
|
|
13
|
++
--
+
-
!
~
( type )
|
Unary pre-increment
Unary pre-decrement
Unary plus
Unary minus
Unary logical negation
Unary bitwise complement
Unary type cast
|
Right to left
|
|
12
|
*
/
%
|
Multiplication
Division
Modulus
|
Left to right
|
|
11
|
+
-
|
Addition
Subtraction
|
Left to right
|
|
10
|
<<
>>
>>>
|
Bitwise left shift
Bitwise right shift with sign extension
Bitwise right shift with zero extension
|
Left to right
|
|
9
|
<
<=
>
>=
instanceof
|
Relational less than
Relational less than or equal
Relational greater than
Relational greater than or equal
Type comparison (objects only)
|
Left to right
|
|
8
|
==
!=
|
Relational is equal to
Relational is not equal to
|
Left to right
|
|
7
|
&
|
Bitwise AND
|
Left to right
|
|
6
|
^
|
Bitwise exclusive OR
|
Left to right
|
|
5
|
|
|
Bitwise inclusive OR
|
Left to right
|
|
4
|
&&
|
Logical AND
|
Left to right
|
|
3
|
||
|
Logical OR
|
Left to right
|
|
2
|
&:
|
Ternary conditional
|
Right to left
|
|
1
|
=
+=
-=
*=
/=
%=
|
Assignment
Addition assignment
Subtraction assignment
Multiplication assignment
Division assignment
Modulus assignment
|
Right to left
|
|