Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

PROGRAMMIN

G IN JAVA
PRECEDENCE RULES AND ASSOCIATIVITY
Precedence Rules and Associativity
Precedence rules are used to determine the order of
evaluation priority in case there are two operators with
different precedence.
Associativity rules are used to determine the order of
evaluation if the precedence of operators is same.
Associativity Types
Associativity is of two types: Left and Right.
Left associativity means operators are evaluated from left to
right and vice versa for right associativity.
Precedence and associativity can be overridden with the help
of parentheses.
Here L-> R indicates associativity from left to right and R->L
indicates associativity from right to left.
Operator Description Precedence Associativity

() []. Parentheses, Array


Subscript, Member 16 Left to Right
Function

Unary Post Increment,


++ -- 15 Right to Left
Unary Post Decrement

++,--,+, -, !, ~ Unary(Pre Increment,


Pre Decrement, Plus,
minus, Negation, 14 Right to Left
Complement)

(), new Cast, Object Creation 13 Right to Left

Multiplication,
*, /,%,+, - 12 Left to Right
Division, Modulus

+, - Addition, Subtraction 11 Left to Right


Operator Description Precedence Associativity

>>, << Bitwise Right Shift /


Bitwise Left Shift 10 Left to Right

<,<=,>,>= Less than, Less than or


instanceof equal, Greater than,
Greater than or equal, 9 Left to Right
Type Comparison
==, != Equal to, Not equal to 8 Left to right
& Bitwise AND 7 Left to Right
^ Bitwise XOR 6 Left to Right
| Bitwise OR 5 Left to Right
&& Logical AND 4 Left to Right
|| Logical OR 3 Left to Right
?: Ternary Conditional 2 Right to Left
Assignment,
Shorthand Assignment
=, +=, -=, *= , /=, %= 1 Right to Left
operators
Example for Precedence Rules
and Associativity
int x = 5, y = 10, z = 3
int ans = x + y * z / 10;
Output
8
Reason:
The precedence of * and / is greater than that of + operator
hence they will be evaluated.
Also, * and / are of the same precedence so they will be
associated in left to right order.
Therefore – 𝑎𝑛𝑠=𝑥+𝑦∗𝑧/10 is
equivalent to 𝑎𝑛𝑠=𝑥+((𝑦∗𝑧)/10)
Example for Precedence Rules
and Associativity
int x = 10, y = 20, z = 5;
int ans = x + --y - ++z;
System.out.println(ans);
Output
23
The precedence of ++ and -- is greater than that of -, and
+ operators.

Hence, 𝑎𝑛𝑠=𝑥+−−𝑦−++𝑧 is equivalent to

𝑎𝑛𝑠=𝑥+(−−𝑦)−(++𝑏)
Primitive Type Conversion and Casting
In Java, type conversions are performed automatically when
the type of the expression on the right-hand-side of an
assignment operation can be safely promoted to the type of the
variable on the left-hand-side of the assignment.
Widening conversions
Conversions that are implicit in nature are termed as widening
conversions.
Widening is the conversion of a smaller data type into a larger
one. This type of casting is done automatically by Java and
does not require any explicit syntax.

Example:
byte b = 10; // byte variable
int i = b; // implicit widening byte to int
Widening conversions
Type conversion or promotion also takes place while
evaluating the expressions involving arithmetic operators.

Example:
int i = 10; //int variable
double d = 20; //int literal assigned to a double variable
d = i + d; //automatic conversion int to double
Widening conversions
public static void main(String[] args)
{
byte a=70;
short b=a;
int c=a;
long d=a;
float e=a;
double f=a;
System.out.println(a+" is of byte datatype"); System.out.println(b+"
is of short datatype"); System.out.println(c+" is of int datatype");
System.out.println(d+" is of long datatype"); System.out.println(e+"
is of float datatype"); System.out.println(f+" is of double datatype");
}
Widening conversions
Primitive Type Casting
Type Casting is not implicit in nature.
It has to be explicitly mentioned by preceding it with the
destination type specified in the parentheses.
For instance, int i = (int)(8.0/3.0);
Casting is also known as narrowing conversion
(reverse of widening conversion).
Narrowing conversions
Narrowing is the conversion of a larger data type into
a smaller one.
Unlike widening, this type of casting requires explicit syntax.
Narrowing conversions

You might also like