Operators Precedence and Associativity in Java

Operator Definition Precedence Associativity
[] Accessing Array
() Methods calls 0(Highest) Left-to-Right
. Access Object Members
expression++ Post-Increment
expression-- Post-Decrement
--expression Pre-Decrement
++expression Pre-Increment 1 Right-to-Left
! Logical Not
~ Bitwise Negation
+ Unary Addition
- Unary Subtraction
() Type-Casting
new Creating Object 2 Right-to-Left
* Arithmetic Multiplication
/ Arithmetic Division 3 Left-to-Right
% Modulus Operator
+ Arithmetic Addition
- Arithmetic Subtraction 4 Left-to-Right
+ String Concatenation
<< Bitwise Left Shift
>> Bitwise Sign Right Shift 5 Left-to-Right
>>> Bitwise Unsigned Right Shift
< Relational less than
<= Relational less than or equal
> Relational greater than 6 Left-to-Right
>= Relational greater than or equal
instanceof Object comparison
== Relational is equal to
!= Relational is not equal to 7 Left-to-Right
& Bitwise AND 8 Left-to-Right
^ Bitwise XOR 9 Left-to-Right
| Bitwise OR 10 Left-to-Right
&& Logical AND 11 Left-to-Right
|| Logical OR 12 Left-to-Right
?: Ternary Operator 13 Right-to-Left
= Assignment
+= Addition Assignment
-= Subtraction Assignment
*= Multiplication Assignment
/= Division Assignment 14 Right-to-Left
%= Modulus Assignment
<<= Left-Shift Assignment
>>= Signed Right-Shift Assignment
>>>= Unsigned Right-Shift Assignment
, Comma 15(Lowest) Right-to-Left


Increment Decrement Operators
Operator Definition Associativity
expression++ Post-Increment
expression-- Post-Decrement Right-to-Left
--expression Pre-Decrement
++expression Pre-Increment

int i = 10, j = 20, k = 30, l = 40;
int x;
x = i++; //x = 10 post-increment after this statement i value is 11.
x = j--; //x = 20 post-decrement after this statement j value is 19.
x = ++k; //x = 31 pre-increment and k value is also 31.
x = --l; //x = 39 pre-decrement and l value is also 39.

int a = 100;
int p = ++a - a++ //a = 102 and p = 0

Since associativity is Right-to-Left and precedence of ++ is higher than -.

p = 101 - 101
a = 102
p = 0

int m = 100;
int n = ++m - m++ + m++ //m = 103 and n = 102


Arithmetic Operators
Operator Definition Precedence Associativity
* Arithmetic Multiplication
/ Arithmetic Division 3 Left-to-Right
% Modulus Operator
+ Arithmetic Addition
- Arithmetic Subtraction 4 Left-to-Right
+ String Concatenation

int x = 5 + 6 - 4 + 9 - 14;

Step 1: 11 - 4 + 9 - 14
Since associativity of + and - are Left-to-Right and first Addition and Subtraction is evaluated because their precedence is higher than assignment.
Step 2: 7 + 9 - 14
Step 3: 16 - 14
Step 4: 2


Shift Operators
Shift Operators:
  1. Left Shift ( << )
  2. Right Shift ( >> )
  3. Unsigned Right Shift ( >>> )

package com.java.basics;

public class ShiftOperator {

    public static void main(String[] args) {
        int x = 1;
        //Binary representation for negative number
        //11111111111111111111111111111111
        System.out.println(Integer.toBinaryString(~x + 1));
        
        //Left shift 16
        System.out.println(x << 4);
        //80000000
        System.out.println(Integer.toHexString(x << 31));
        
        //Right shift
        int y = 0xff;
        System.out.println(y >> 4);  //15
        
        System.out.println(-y >> 4);  //-16
        System.out.println((~y + 1) >> 4); //-16
        
        System.out.println(-y >>> 30);  //3
        System.out.println((~y + 1) >>> 30); //3
    }
}


Equality and Relational Operators
Operator Definition Precedence Associativity
< Relational less than
<= Relational less than or equal
> Relational greater than 6(Higher) Left-to-Right
>= Relational greater than or equal
instanceof Object comparison
== Relational is equal to
!= Relational is not equal to 7(Lower) Left-to-Right

Precedence of relational operators is higher than equality operators.
Relational and equality expressions either evaluate to true or false.

int a = 25, b = 26, c = 25;

a > b  //false since 25 is not less than to 26
a < b  //true since 25 is  less than 26
a >= c //true since 25 is greater than or equal to 25
a <= b //true since 25 is less than or equal to 26

String str = "Shyam";
str instanceof String //true since str object is instance of String class.

a == c //true since 25 is equal to 25
a == b //false since 25 is not equal to 26
a != b //true since 25 is not equal to 26
a != c //false since 25 is equal to 25

Bitwise AND (&), Bitwise XOR (^), Bitwise OR (|) and Bitwise NOT (~)
A B A & B A ^ B A | B ~A ~B
0 0 0 0 0 1 1
0 1 0 1 1 1 0
1 0 0 1 1 0 1
1 1 1 0 1 0 0

Logical AND (&&), Logical XOR (^), Logical OR (||) and Logical NOT (!)
A B A && B A ^ B A || B !A !B
False False False False False True True
False True False True True True False
True False False True True False True
True True True False True False False

Conditional Operator (Ternary Operator)
Operator Definition Associativity
?: Ternary Operator Right-to-Left

Precedence of Ternary operator (?:) is only higher than assignment and comma operators and it's associativity is from right to left.

Syntax:
result = expression ? value1 : value 2;

If the expression evaluate to true then value1 is assigned to result else value2 is assigned to result.

boolean res = 10 > 20 ? true : false;

Since 10 is not greater than 20 so false is assigned to res.

int year = 2016;
boolean isLeapYear = year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ? true : false;

expression = year % 4 == 0 && year % 100 != 0 || year % 400 == 0

Step 1: 0 == 0 && 16 != 0 || 16 == 0
Since modulus(%) operator has highest precedence among all operators.
Step 2: true && true || false
Since next highest precedence is equality operators
Step 3: true || false
Since Logical AND (&&) has higher precedence than Logical OR (||)
Step 4: true

Assignment Operator
Operators Definition Associativity
= Assignment
+= Addition Assignment
-= Subtraction Assignment
*= Multiplication Assignment
/= Division Assignment Right-to-Left
%= Modulus Assignment
<<= Left-Shift Assignment
>>= Signed Right-Shift Assignment
>>>= Unsigned Right-Shift Assignment

int number1;
int number2;
number1 = number2 = 10;

First 10 will assign to number2 then number2 value assign to number1 because = Associativity is Right-to-Left.

String str = "Hello World";

String literal "Hello World" is assign to str.

double d = 10.5 + 15.25;

First Addition of 10.5 and 15.25 is evaluated then the result is assign to d because + has higher Precedence than = .

long num = 111;
long num += 50 * 20;

First multiplication of 50 and 20 is evaluated then the result is added to num and assign to num again because * has higher Precedence than += .

Note:
There is no space between + and = in += Operator.

No comments:

Post a Comment