0% found this document useful (0 votes)
49 views

Operators

The document discusses operator precedence and associativity in Java. It explains that operators with higher precedence are executed before those with lower precedence. If operators have the same precedence, their associativity determines the order of execution from left to right or right to left. The document also categorizes Java operators and provides examples of arithmetic, relational, logical, and assignment operators.

Uploaded by

nhs911
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Operators

The document discusses operator precedence and associativity in Java. It explains that operators with higher precedence are executed before those with lower precedence. If operators have the same precedence, their associativity determines the order of execution from left to right or right to left. The document also categorizes Java operators and provides examples of arithmetic, relational, logical, and assignment operators.

Uploaded by

nhs911
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

Operators

• The sequence in which different operators in an


expression are executed is determined by the
precedence of the operators.
• Operators with a higher precedence are executed
before operators with a lower precedence.
• In the following table, operators are in descending
order of precedence, so those with the highest
precedence are at the top.
• Operators within the same group in the table are of
equal precedence.

Operators tMyn 1
• The sequence of execution of operators with the
same precedence in an expression is determined
from their associativity.
• The associativity of an operator determines how it
groups with its operands in an expression.
• Operators may be left-associative or right-
associative.

Operators tMyn 2
• All unary operators and all assignment operators are
right associative.
• All other operators are left associative.
• With the throw operator (exception throwing) the
associativity is not available.

Operators tMyn 3
• Associativity is only needed when the operators in an
expression have the same precedence. Usually +
and - have the same precedence. Consider the
expression 7-4+2. The result could be either (7-4)+2
= 5 or 7-(4+2) = 1. The former result corresponds to
the case when + and - are left-associative. The latter
result corresponds to the case when + and - are right-
associative. Operators with the same precedence
always have the same associativity. The operators +,
-, * and / are left-associative.

Operators tMyn 4
• A left-associative operator can be said to associate
"to the left", and similarly a right-associative operator
can be said to associate "to the right". To understand
the intuition behind these names, consider again the
expression 7-4+2. If + and - are left-associative then
the middle operand (4) belongs to the operator on its
left (hence the name "to the left"). If + and - are right-
associative then the middle operand belongs to the
operator on its right (hence the name "to the right").

Operators tMyn 5
• A left-associative operator may also be said to have
"left to right" associativity, and a right-associative
operator may also be said to have "right to left"
associativity. This is somewhat counter-intuitive
considering the above paragraph. To understand the
intuition behind these names consider the expression
1+2+3+4+5. If + is left-associative, the addition
operations are carried out left to right, i.e. the result is
(((1+2)+3)+4)+5. If + is right-associative, the addition
operations are carried out right to left, i.e. the result is
1+(2+(3+(4+5))).

Operators tMyn 6
Operator Precedence

postfix expr++ expr--


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

Operators tMyn 8
• An operator is a symbol that tells the compiler to
perform a specific mathematical or logical
manipulation.
• Java has four general classes of operators:
arithmetic, bitwise, relational and logical.

Operators tMyn 9
• Arithmetic operators

Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
++ Increment
-- Decrement

Operators tMyn 10
• Relational and Logical Operators

• Relational refers to the relationships that values can


have with one another, and logical refers to the ways
in which true and false values can be connected
together.
• Since the relational operators produce true or false
results, they often work with the logical operators.

Operators tMyn 11
• The relational operators

Operator Meaning
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or
equal to
<= Less than or equal to

Operators tMyn 12
• The logical operators

Operator Meaning
& AND
| OR
^ XOR(exclusive OR)
|| Short-circuit OR
&& Short-circuit AND
! NOT

Operators tMyn 13
• Java supplies special short-circuit versions of its AND
and OR logical operators that can be used to produce
more efficient code.
• In an AND operation, if the first operand is false, the
outcome is false no matter what value the second
operand has.
• In an OR operation, if the first operand is true, the
outcome of the operation is true no matter what the
value of the second operand.
• Thus, in these two cases there is no need to evaluate
the second operand. By not evaluating the second
operand, time is saved and more efficient code is
produced.

Operators tMyn 14
• The short-circuit AND operator is &&, and the short-
circuit OR operator is ||. Their normal counterparts
are & and |.
• The only difference between the normal and short-
circuit versions is that the normal operands will
always evaluate each operand, but short-circuit
versions will evaluate the second operand only when
necessary.

Operators tMyn 15

You might also like