link one

link one

link one

link one

link one

link one

2.6 - Operators

In programming languages, operators are tiny predefined utility programs devised for accomplishing some tasks like mathematical and logical operations, shifting the bits, assigning data to variables etc. These programs are called with placing some tokens like + and the other predefined operator tokens. In some programming languages especially in Ada, users can change the meaning of these operators with user defined programs. However, that may turn on to an adventureous incentive because it may start some difficult to predict side- effects. Fortunately, in Java, users have not allowed to redefine these programs and they will act exactly according to their definitions. Operators are designed for acting upon their operands. OPerators may act upon one or more operands. Operators needing only one operand are called unary operators. The majority of the operators operate on two operands, these are binary operators. Only one operator has three operands, this is the ternary operator. Java operators are listed below:

2.6 Table 1 - Operator Precedence
(From top to bottom, top:higher precedence)
Operators Application
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 = += -= *= /= %= &= ^= |= <<= >>= >>>=

This list is based on an official Oracle source. The precedence indicate which of the operators evaluates first. Precedence is higher up to the top of the list. Operators which are in the same line have equal precedence, Operators evaluate from left to right except assignment operators which evaluates from right to left. In the expressions where operators of equal precedence is involved. The developer must arrange for supressing the ambiguity. This is done by using parenthesis properly. In general use paranthesis, wherever a precise order of evaluation may seem somewhat obscure..

Another compilation of Java operator precedence is presented below. This not an official publication but may be from here, we can understand operator precedence more clearly.

Note: Use explicit parentheses in the possibility of confusion.

2.6 Table -2 The operators in Java, shown in order of precedence - from highest to lowest
Priority Operators Operation Associativity
1 [ ] array index left
() method call
. member access
2 ++ pre- or postfix increment right
-- pre- or postfix decrement
+ - unary plus, minus
~ bitwise NOT
! boolean (logical) NOT
(type) type cast
new object creation
3 * / % multiplication, division, remainder left
4 + - addition, substraction left
+ string concatenation
5 << signed bit shift left left
>> signed bit shift right
>>> unsigned bit shift right
6 < <= less than, less than or equal to left
> >= greater than, greater than or equal to
instanceof reference test
7 == equal to left
!= not equal to
8 & bitwise AND left
& boolean (logical) AND
9 ^ bitwise XOR left
^ boolean (logical) XOR
10 | bitwise OR left
| boolean (logical) OR
11 && boolean (logical) AND left
12 || boolean (logical) OR left
13 ? : conditional right
14 = assignment right
*= /= += -= %=
<<= >>= >>>=
&= ^= |=
combinated assignment
(operation and assignment)

The table below is more accurate and compiled by David Flanagan in Java in a Nutshell 5Th ed.

2.6 Table-3 Priority Table (D.Flanagan)
Priority Associativity Operator Operand Type(s) Operation Performed
15 L . object, member object member access or object dereferencing
    [] array, int array element access
    (args) method, arglist method invocation
    ++, − − variable post-increment, decrement
14 R ++, − − variable pre-increment, decrement
    +, − number unary plus, unary minus
    ~ integer bitwise complement
    ! boolean boolean NOT
13 R new class, arglist object creation
    (type) type, any cast (type conversion)
12 L *, /, % number, number multiplication, division, remainder
11 L +, − number, number addition, subtraction
    + string, any string concatenation
10 L << integer, integer left shift
    >> integer, integer right shift with sign extension
    >>> integer, integer right shift with zero extension
9 L <, <= number, number less than, less than or equal
    >, >= number, number greater than, greater than or equal
    instanceof reference, type type comparison
8 L = = primitive, primitive equal (have identical values)
    != primitive, primitive not equal (have different values)
    = = reference, reference equal (refer to same object)
    != reference, reference not equal (refer to different objects)
7 L & integer, integer bitwise AND
    & boolean, boolean boolean AND
6 L ^ integer, integer bitwise XOR
    ^ boolean, boolean boolean XOR
5 L | integer, integer bitwise OR
    | boolean, boolean boolean OR
4 L && boolean, boolean conditional AND
3 L || boolean, boolean conditional OR
2 R ?: boolean, any, any conditional (ternary) operator
1 R = variable, any assignment
    +=, −=, *=, /=, %=, &=, |=, ^=, >>=, <<=, >>>= variable, any assignment with operation

In this section we may not be able to examine all the operators listed in above tables. Some of the listed operators like method call, instanceof, object creation will be introduced in the section of Object Oriented Programming (OOP). We will introduce here only the operators that are used in core Java language topics and these cover most of the operators listed in the above priority tables.

<< Index

Polyglott HTML5(XHTML5 compliant HTML5 code)