Monday, 14 January 2013

Java Operators

Operators in Java

Operators Supported by Java Language


Operators are special symbols that perform specific operations on one, two, or three operands, and then return a result. Java supports a rich set of operators. Some of them are =, +, -, *. An Operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. Operators are used in programs to manipulate data and variables. They usually form a part of mathematical or logical expressions. 
Java operators can be classified into a number of related categories as below:

Arithmetic Operators
+      Additive operator (also used for String concatenation)
-       Subtraction operator
*       Multiplication operator
/       Division operator
%     Remainder operator


Relational Operators
==    Equal to
!=     Not equal to
>      Greater than
>=    Greater than or equal to
<      Less than
<=    Less than or equal to


Logical Operators
&&   Logical AND
||       Logical OR
!       Logical NOT


Assignment Operators
=      Assignment

Increment and decrement Operators
++   Adds 1 to the Operand
--     Subtracts 1 from the Operand

Conditional Operators
?:    Ternary (shorthand for if-then-else statement)

Bitwise Operators
~     Unary bitwise complement
<<   Signed left shift
>>   Signed right shift
>>> Unsigned right shift
&     Bitwise AND
^      Bitwise exclusive OR
|       Bitwise inclusive OR

Special Operators
. (Dot Operator)   - To access instance variables
instanceof             - Object reference Operator

As we explore the operators of the Java programming language, it may be helpful for you to know ahead of time which operators have the highest precedence. The operators in the following table are listed according to precedence order. The closer to the top of the table an operator appears, the higher its precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence. Operators on the same line have equal precedence. When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.

Operator Precedence
Operators Precedence
postfix expr++ expr--
unary ++expr --expr +expr -expr ~ !
multiplicative * / %
additive + -
shift << >> >>>
relational < > <= >= instanceof
equality == !=
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
logical OR ||
ternary ? :
assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=

The basic evaluation procedure includes two left-to-right passes through the expression. During the first pass, the high priority operators (if any) are applied as they are encountered. During the second pass, the low priority operators (if any) are applied as they are encountered.

No comments :

Post a comment