## DEV Community is a community of 639,856 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

loading...

# JAVA: theory of Operators, Precedence & Associativity Supriya Kolhe Updated on ・3 min read

Q1: What are the operators?
Q2: Types?
Q3: Arithmetic operators?
Q4: Assignment Operators?
Q5: Relational Operators?
Q6: Logical Operators?
Q7: Unary Operators?
Q8: Bitwise Operators?
Q9: instanceof Operator?
Q10: Ternary Operator?
Q11: Precedence and Associativity of Operators

1. What are the operators:-
-symbols that perform operations on variables and values. For example, + is used to perform an addition operation.

2. Types:-

1. Arithmetic Operators
2. Assignment Operators
3. Relational Operators
4. Logical Operators
5. Unary Operators
6. Bitwise Operators
7. instanceof Operator
8. Ternary Operator

3. Arithmetic Operators:-

• these operators are used to perform basic mathematical operations on variables or data. For example, a+b or so
• Addition : a+b, here, a is added with b variable
• Subtraction : a-b, here, b is subtracted from b variable
• Multiplication : a*b, here, a is multiplied wth b variable
• Division : a/b, here, b is divided by a
• Modulus : a%b, here, remainder after division of b by a is passes

4. Assignment Operators:-
-used to store the value in variable right after performing basic mathematical operations

1. = : Assignment, used to assign values to variables. a=6 will store 6 value in a variable. It assigns the value on it's the right side to it's left.
2. += : Addition Assignment, a += b will work as a = a + b;
3. -= : Subtraction Assignment, a -= b; will work as a = a - b;
4. *= : Multiplication Assignment, a *= b; will work as a = a * b;
5. /= : Division Assignment, a /= b; will work as a = a / b;
6. %= : Modulus Assignment, a %= b; will work as a = a % b;

5. Relational Operators:-

1. == : Is Equal To, 3 == 9 returns false
2. != : Not Equal To, 8 != 5 returns true
3. > : Greater Than, 1 > 5 returns false
4. < : Less Than, 1 < 5 returns true
5. >= : Greater Than or Equal To, 1 >= 5 returns false
6. <= : Less Than or Equal To, 1 <= 5 returns false

6. Logical Operators:-

1. && : logical AND, expression1 && expression2, return true only if both expressions are true
2. || : logical OR, expression1 || expression2, return true if at least one expression is true
3. ! : logical NOT, !expression, return true if expression is not true or false

7. Unary Operators:-
-these operators are used only with one operand.

1. + : Unary plus, stores value as positive, but there is no need to use it since without any operator the value is taken as positive
2. - : Unart minus, convert a negative value to a positive one, a = -10
3. ! : Logical complement operator, inverts the value of a boolean,
4. ++ : Increment operator, increments value by 1
5. -- : Decrement operator, decrements value by 1

NOTE: please check Java: Increment, Decrement operator for detailed ++ and --.

8. Bitwise Operators:-
-used to perform operations on individual bits
-can be used with any of the integer types

1. ~ : Bitwise Complement, return 1n's complement
2. & : Bitwise AND. returns bit by bit AND of input values
3. | : Bitwise OR, returns bit by bit OR of input values
4. ^ : Bitwise XOR, returns bit by bit XOR of input values
5. << : Left Shift Operator, shifts all bits towards the left by a certain number of specified bits. For example, number<<2 where 2 means 2-bit operation
6. <<< : Unsigned Left shift operator, since "<<<" and "<<" are similar, there is no "<<<" operator in java.
7. >> : Signed Right Shift Operator, shifts all bits towards the right by a certain number of specified bits, For example, number>>2 where 2 means 2-bit operation
8. >>> : Unsigned Right Shift Operator

9. instanceof:-
-checks whether an object is an instanceof a particular class
-For example, output = str instanceof String;

10. Ternary Operator:-
-short trick for "if-then-else" statement
-Syntax: variable = expression ? expression1 : expression2
OR you can also say,
variable = condition ? if_execution_block: else_execution_block

11. Precedence and Associativity of Operators:-
-higher it appears in the table, the higher its precedence
-If an expression has more than one operators with similar precedence, the expression is evaluated according to its associativity i.e. left to right or right to left. For example, x=y=z=a, here allocation will be followed from right to left. -General doubts on Precedence and Associativity:

1. using + in System.out.println(): in order to perform addition operation in System.out.println(), one should make sure to add them in parenthesis. Now the question arises why? Well, associativity of addition is left to right and hence integers are added to a string first and so forth.
2. is something like a=n+++o right? the answer is yes because it is understood by the compiler as a, =, n, ++, +, 0. But something like a=b+++++c will not work since there is no operand after the second unary operator.

Please comment if you have any feedback or suggestions