# Operators in java programming language

In this section 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 operator is present in the same calculation.

## Operator Precedence Table

OperatorsPrecedence(High to Low)
postfixexpr`++ `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

```/*
* Here we will see the effect of precedence in operators life
*/
class OperatorPrecedenceExample {

public static void main(String args[]) {
int i = 40;
int j = 80;
int k = 40;

int l = i + j / k;
/*
* In above calculation we are not using any bracket. So which operator
* will be evaluated first is decided by Precedence. As precedence of
* divison(/) is higher then plus(+) as per above table so divison will
* be evaluated first and then plus.
*
* So the output will be 42.
*/

System.out.println("value of L :" + l);

int m = (i + j) / k;
/*
* In above calculation brackets are used so precedence will not come in
* picture and plus(+) will be evaluated first and then divison()/. So
* output will be 3
*/

System.out.println("Value of M:" + m);
}
}```

## Operator Associativity

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

## Example of Associativity

```package jbt.bean;

/*
* Here we will see the effect of precedence in operators life
*/
public class OperatorAssociativityExample {

public static void main(String args[]) {
int i = 40;
int j = 80;
int k = 40;

int l = i / k * 2 + j;
/*
* In above calculation we are not using any bracket. And there are two
* operator of same precedence(divion and multiplication) so which
* operator(/ or *) will be evaluated first is decided by association.
* Associativity of * & / is left to right. So divison will be evaluated
* first then multiplication.
*
* So the output will be 82.
*/

System.out.println("value of L :" + l);

int m = i / (k * 2) + j;
/*
* In above calculation brackets are used so associativity will not come
* in picture and multiply(*) will be evaluated first and then
* divison()/. So output will be 80
*/

System.out.println("Value of M:" + m);
}

}```

## Operators in Java

Let us discuss each operator individually.

Assignment (=) and  Arithmetic operators(+, -, *, /) work the same way as they do in other programming languages, so we will not discuss them here. The precedence for  ‘/‘  and  ‘*‘ operators is higher than sum(+) or minus() or modular division(%)

Series Navigation<< Non Access Modifiers in JavaJava Statements tutorial for Beginners >>
By | 2017-08-14T10:57:59+00:00 September 9th, 2012|Core Java|51 Comments

### About the Author: J Singh

1. atebeni cherotwo October 3, 2012 at 6:07 pm - Reply

This is the real life i want!!!!!!!!!!!!!1

• Riyaz June 24, 2016 at 9:37 am - Reply

Explanation is good.

2. Raj January 11, 2013 at 5:26 am - Reply

Accidentally came to this site. but not able to leave the site:) nicely written, nice to read. Thanks!!!

• Sujay August 7, 2014 at 5:18 pm - Reply

Nice website….(y)

• Vivekanand Gautam August 7, 2014 at 6:01 pm - Reply

Hi Sujay,

Thanks for your nice comment. 🙂

Regards

• Mayank Srivastava April 2, 2015 at 10:43 am

Bhai.. good work. Will finish up quickly everything , from start to end.
Everything is compiled so nicely , that’s required for each developer to know.

• Nabiafsar July 12, 2015 at 9:30 am - Reply

Hi hill

• tushar November 18, 2016 at 9:54 am - Reply

same here

3. swetha February 7, 2013 at 11:55 am - Reply

i love this website….i found a clear description in each and every topic
thanku

4. skumar March 8, 2013 at 6:15 am - Reply

very nice each and every point explained in detail

5. vignesh March 28, 2013 at 10:28 am - Reply

i love to study in this website thank you

6. Rashmi April 6, 2013 at 9:31 am - Reply

very nice website.. very thankful to u ..

7. elangovan May 9, 2013 at 8:34 am - Reply

Really nice web site for learning java

8. randki July 10, 2013 at 9:30 pm - Reply

Do you mind if I quote a few of your posts as long as I provide credit and sources back to
your webpage? My blog is in the exact same niche as yours and
my visitors would truly benefit from some of the information you provide
here. Please let me know if this okay with you. Thank you!

Yes, You can include the post as long as you provide the proper credit.
Thanks

9. ansarali July 22, 2013 at 1:10 am - Reply

easy to learn keep doing guys..

10. senthilkumar January 1, 2014 at 5:07 am - Reply

good programs..

11. vidya January 28, 2014 at 5:20 am - Reply

learning java became soo easy.. !!! i love this website..!!! this helps beginners alot.. !! expecting more from you… 🙂

12. Saravanan April 11, 2014 at 6:32 am - Reply

very simple and easy to learn… thank you

HI gautam, i have a doubt that what are the things which will come outside the main and what are the things will come inside the main method, there is a huge confusion about this and where the program actually starts??? plz kindly help me……………….. waiting for the reply

14. Param Gates May 22, 2014 at 9:07 am - Reply

I wanted to learn Java. I googled and reached your site. Your syllabus is very easy to understand and can be self learned. Thanks for the site and wishes for your hardwork.

15. Amruta August 16, 2014 at 6:10 pm - Reply

Simple and Clear and Comprehensive.
Needed a quick review of core java and found it here.

16. Padmanabha Reddy V November 11, 2014 at 10:03 am - Reply

NICE SITE.THE MATTER IS EASY TO UNDERSTAND AND LEARN.

17. Akshay January 22, 2015 at 1:19 pm - Reply

Really Nice Website for Beginners……….(Y)
Every concept explained in detaill and easy to understand way………….

• Vivekanand Gautam January 24, 2015 at 4:53 am - Reply

Thanks Akshay 🙂

18. Anjali Kriti January 30, 2015 at 7:32 am - Reply

Can you explain me the function of this shift >>> operator?

• Vivekanand Gautam January 31, 2015 at 1:53 pm - Reply

• sid April 17, 2015 at 7:40 pm - Reply

Very nice website for beginners.

19. Abdul Razaque Soomro February 17, 2015 at 8:25 pm - Reply

this website helpful for us and thank u sir

20. Abdul Razaque Soomro February 17, 2015 at 8:27 pm - Reply

this website very helpful for us and sir thank u

21. Murali February 20, 2015 at 7:00 am - Reply

Hi Sir Really This website is good referral website for beginners

22. Aaisha March 11, 2015 at 5:07 pm - Reply

Could you please explain to me the following expression

int m=i/(k*2)+j;
(precedence doesn’t come into action and that is understood)
While evaluating the expression this is what happens(40/(40*2)+80)=(40/(80)+80)==(1+80)==81
Always the (/) is considered to give us the quotient.

23. Parthiban P March 12, 2015 at 12:12 am - Reply

Really very useful for beginners…

Just let me know some other same tuorials for SQL and Javascirpt….

24. ARUN April 16, 2015 at 12:14 pm - Reply

i want basic java interview questions …….

25. abhishek April 30, 2015 at 7:16 am - Reply

sir can u plz mail me some shift operator programes.

This site is incredible!!!!!!!…. This is what a beginner is looking for… Sooo easy and worthy…. Kudos Author

27. Raj June 3, 2015 at 5:18 am - Reply

Plss give output of the programs…

28. girish pathrabe June 5, 2015 at 5:25 am - Reply

this is called perfect java tutorial. the site is awesome . clearely explain each n every concept

29. Yamini Sukhija June 9, 2015 at 7:26 pm - Reply

Great Work!

30. arpitha June 10, 2015 at 5:05 am - Reply

In my side opinion is examples will be given on understanding purpose but that examples will be not understandable way.try to get user understandable examples.

31. arpitha June 10, 2015 at 5:06 am - Reply

good examples are not giving you.

32. andrew June 10, 2015 at 10:32 am - Reply

Nice coding n commenting …..well structured organized, easy to understand….thanks for big

33. nikita June 21, 2015 at 3:51 pm - Reply

nicely structured and well understood ….thankyou

34. rams June 27, 2015 at 6:38 am - Reply

Today I stated the learning by seeing this blog, I am the new one to programming. Your blessing is required

35. Ankita July 1, 2015 at 3:50 am - Reply

Excellent site…it helps a lot.

36. Jyothy September 12, 2015 at 6:40 pm - Reply

Very good site. It includes good details about Core Java. I am learning Core Java at the moment. i would like to complete Core Java and also rest of the sections.

Thanks for good compilation with good examples.

37. Thilina Sampath March 17, 2016 at 4:54 am - Reply

m = i / (k * 2) + j;
there m=80 is incorrect.

• J Singh March 18, 2016 at 7:18 pm - Reply

Value will be 80 only. Please check once again.

38. saran April 9, 2016 at 1:19 pm - Reply

nice good…