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

Operator Precedence Table

This document discusses operators in Java programming language. It covers operator precedence and associativity. For operator precedence, it provides a table showing the precedence from highest to lowest. For example, multiplication and division have higher precedence than addition and subtraction. It also discusses how associativity determines the order of operations when two operators have the same precedence level, such as left to right for multiplication and division. Examples are provided to illustrate precedence and associativity.

Uploaded by

NisargaTewniks
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Operator Precedence Table

This document discusses operators in Java programming language. It covers operator precedence and associativity. For operator precedence, it provides a table showing the precedence from highest to lowest. For example, multiplication and division have higher precedence than addition and subtraction. It also discusses how associativity determines the order of operations when two operators have the same precedence level, such as left to right for multiplication and division. Examples are provided to illustrate precedence and associativity.

Uploaded by

NisargaTewniks
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Operators in java programming language

09. Sep

Core Java

22 Comments

In this chapter we will learn about Operator Precedence and Operator


Associativity.

Operator Precedence
Precedence decides which operator will be evaluated first in a case where
more than one operators are present in the same calculation.

Operator Precedence Table


Operators

Precedence(High to Low)

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

= += -= *= /= %= &= ^= |= <<= >>= >>>=

Example of Precedence
1 /*
2 * Here we will see the effect of precedence in operators life
3 */
4 class OperatorPrecedenceExample {
5
6 public static void main(String args[]) {
7 int i = 40;
8 int j = 80;
9 int k = 40;
1
0 int l = i + j / k;
1 /*
1 * In above calculation we are not using any bracket. So which operator
1 * will be evaluated first is decided by Precedence. As precedence of
2 * divison(/) is higher then plus(+) as per above table so divison will
1 * be evaluated first and then plus.
3 *
1 * So the output will be 42.
4 */
1
5 System.out.println("value of L :" + l);
1
6 int m = (i + j) / k;
1 /*
7 * In above calculation brackets are used so precedence will not come in
1 * picture and plus(+) will be evaluated first and then divison()/. So
8 * output will be 3
1 */
9
2 System.out.println("Value of M:" + m);
0 }
2 }
1
2
2
2
3
2
4

2
5
2
6
2
7
2
8
2
9
3
0
3
1
3
2

Operator Associativity
If two operators having same precedence exists in the calculation
then Associativity of the operators will be used to decide which operator will
be executed first.

Example of Associativity
1 package jbt.bean;
2
3 /*
4 * Here we will see the effect of precedence in operators life
5 */
6 public class OperatorAssociativityExample {
7
8 public static void main(String args[]) {
9 int i = 40;
1 int j = 80;
0 int k = 40;
1
1 int l = i / k * 2 + j;
1 /*
2 * In above calculation we are not using any bracket. And there are two
1 * operator of same precedence(divion and multiplication) so which
3 * operator(/ or *) will be evaluated first is decided by association.
1 * Associativity of * & / is left to right. So divison will be evaluated
4 * first then multiplication.
1 *
5 * So the output will be 82.
1 */
6
1 System.out.println("value of L :" + l);
7

1
8
1
9
2
0
2
1
2
2
2
3
2
4
int m = i / (k * 2) + j;
2
/*
5
* In above calculation brackets are used so associativity will not come
2
* in picture and multiply(*) will be evaluated first and then
6
* divison()/. So output will be 80
2
*/
7
2
System.out.println("Value of M:" + m);
8
}
2
9
}
3
0
3
1
3
2
3
3
3
4
3
5
3
6

Operators in Java
Let us discuss about each operator individually.
Assignment (=) and Arithmetic operators(+, -, *, /) will work the same way as
they do in other programming language. So we will not discuss about them.

The precedence for / & * operators is higher than sum(+) or minus(-) or


modular division(%)

You might also like