OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 1 : Precedence and Associativity of Operators in Java

 

Operator Precedence

Table 4-1 shows the order of precedence for Java operators, from highest to lowest. Operators in the same row are equal in precedence. In binary operations, the order of evaluation is left to right (except for assignment, which evaluates right to left). Although they are technically separators, the [ ], ( ), and . can also act like operators. In that capacity, they would have the highest precedence.


Using Parentheses

Parentheses raise the precedence of the operations that are inside them. This is often necessary to obtain the result you desire.

For example, consider the following expression:

a >> b + 3

This expression first adds 3 to b and then shifts a right by that result. That is, this expression can be rewritten using redundant parentheses like this:

a >> (b + 3)

However, if you want to first shift a right by b positions and then add 3 to that result, you will need to parenthesize the expression like this:

(a >> b) + 3

In addition to altering the normal precedence of an operator, parentheses can sometimes be used to help clarify the meaning of an expression. For anyone reading your code, a complicated expression can be difficult to understand. Adding redundant but clarifying parentheses to complex expressions can help prevent confusion later.

For example, which of the following expressions is easier to read? a | 4 + c >> b & 7

(a | (((4 + c) >> b) & 7))

One other point: parentheses (redundant or not) do not degrade the performance of your program. Therefore, adding parentheses to reduce ambiguity does not negatively affect your program.


Example Program


public class OperatorPrecedenceDemo {

    public static void main(String[] args) {

        int a = 10, b = 5, c = 2;

        boolean result;


        // Arithmetic + Unary + Assignment

        int x = a + b * c; // b * c = 10, then a + 10 = 20

        System.out.println("x = a + b * c = " + x);


        int y = (a + b) * c; // (10 + 5) * 2 = 30

        System.out.println("y = (a + b) * c = " + y);


        int z = ++a + b--; // ++a = 11, b-- = 5 then becomes 4, so 11 + 5 = 16

        System.out.println("z = ++a + b-- = " + z);

        System.out.println("a after increment = " + a); // 11

        System.out.println("b after decrement = " + b); // 4


        // Bitwise + Shift

        int bitwise = (a & b) | (b << c); // (11 & 4) = 0, (4 << 2) = 16, then 0 | 16 = 16

        System.out.println("(a & b) | (b << c) = " + bitwise);


        // Logical and Relational

        result = (a > b) && (y < z) || !(c == 2); // (true && false) || false = false

        System.out.println("Result of complex logical expression: " + result);


        // Assignment with operators

        int assign = 10;

        assign += 5 * 2; // 10 + (5*2) = 20

        System.out.println("assign += 5 * 2: " + assign);


        // Ternary operator with relational logic

        int max = (a > b) ? a : b;

        System.out.println("Maximum of a and b using ternary: " + max);


        // Shift operators

        int shiftLeft = a << 1; // 11 << 1 = 22

        int shiftRight = a >> 1; // 11 >> 1 = 5

        System.out.println("a << 1 = " + shiftLeft);

        System.out.println("a >> 1 = " + shiftRight);


        // Type casting with arithmetic

        double division = (double) a / c; // 11.0 / 2 = 5.5

        System.out.println("Type casting result: (double) a / c = " + division);


        // instanceof operator

        String str = "Java";

        System.out.println("str instanceof String: " + (str instanceof String));

    }

}


Output:

x = a + b * c = 20

y = (a + b) * c = 30

z = ++a + b-- = 16

a after increment = 11

b after decrement = 4

(a & b) | (b << c) = 16

Result of complex logical expression: false

assign += 5 * 2: 20

Maximum of a and b using ternary: 11

a << 1 = 22

a >> 1 = 5

Type casting result: (double) a / c = 5.5

str instanceof String: true


Comments

Popular posts from this blog

How to Get a Job in Top IT MNCs (TCS, Infosys, Wipro, Google, etc.) – Step-by-Step Guide for B.Tech Final Year Students

Common HR Interview Questions

How to Get an Internship in a MNC