Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java Operator Precedence

In Java, operator precedence defines the order in which operators are evaluated in an expression. Operators with higher precedence are evaluated before operators with lower precedence. If operators have the same precedence, they are evaluated from left to right, following the associativity rule. Here is a list of Java operators in descending order of precedence:

  1. Postfix operators: expr++, expr--
  2. Prefix operators: ++expr, --expr
  3. Unary operators: +, -, !, ~
  4. Type cast: (type) expr
  5. Multiplicative operators: *, /, %
  6. Additive operators: +, -
  7. Bitwise shift operators: <<, >>, >>>
  8. Relational operators: <, >, <=, >=, instanceof
  9. Equality operators: ==, !=
  10. Bitwise AND: &
  11. Bitwise XOR: ^
  12. Bitwise OR: |
  13. Logical AND: &&
  14. Logical OR: ||
  15. Ternary conditional operator: ? :
  16. Assignment operators: =, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>>=

Here's an example that demonstrates Java operator precedence:

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

int result = a + b * c - a / c; // Equivalent to: a + (b * c) - (a / c)

In the example, the multiplication operator * and the division operator / have higher precedence than the addition operator + and the subtraction operator -. Therefore, b * c and a / c are evaluated first, followed by the addition and subtraction operations.

  1. Java Operator Precedence Table:

    • Operator precedence determines the order in which operators are evaluated. Here's a concise table:
    CategoryOperators
    Postfixexpr++ expr--
    Unary++expr --expr +expr -expr ~ !
    Multiplicative* / %
    Additive+ -
    Shift<< >> >>>
    Relational< > <= >= instanceof
    Equality== !=
    Bitwise AND&
    Bitwise XOR^
    Bitwise OR`
    Logical AND&&
    Logical OR`
    Conditional? :
    Assignment`= += -= *= /= %= &= ^=
  2. Operator Precedence in Java Examples:

    • Examples illustrating the order of evaluation based on operator precedence:
    int result = 10 + 5 * 2; // result is 20, multiplication first
    
  3. Java Arithmetic Operator Precedence:

    • Arithmetic operators have higher precedence than most other operators.
    int result = 10 + 5 * 2; // result is 20, multiplication first
    
  4. Logical Operator Precedence in Java:

    • Logical operators (&&, ||) have lower precedence than most other operators.
    boolean result = true || false && true; // result is true, && has higher precedence
    
  5. Bitwise Operator Precedence in Java:

    • Bitwise operators (&, |, ^) have lower precedence than most arithmetic operators.
    int result = 5 & 3 | 1; // result is 5, & has higher precedence
    
  6. Comparison Operator Precedence in Java:

    • Comparison operators (<, >, <=, >=, ==, !=) have higher precedence than logical operators.
    boolean result = 10 > 5 || 3 != 2; // result is true, > has higher precedence
    
  7. Assignment Operator Precedence in Java:

    • Assignment operators (=, +=, -=, *=, /=, %=) have lower precedence than most other operators.
    int x = 5;
    x += 3 * 2; // x is 11, multiplication first, then addition
    
  8. Conditional Operator Precedence in Java:

    • The conditional operator (? :) has lower precedence than most other operators.
    int result = (5 > 3) ? 10 : 5; // result is 10, ternary operator
    
  9. Java Unary Operator Precedence:

    • Unary operators (++, --, +, -, ~, !) have higher precedence than most other operators.
    int x = -5;
    int result = ++x * 2; // result is -8, unary first, then multiplication
    
  10. Java Ternary Operator Precedence:

    • The ternary operator (? :) has lower precedence than most other operators.
    int result = (5 > 3) ? 10 : 5; // result is 10, ternary operator
    
  11. Java Order of Evaluation and Operator Precedence:

    • The order of evaluation is from left to right, respecting operator precedence. Parentheses can be used to override default order.
    int result = 10 + 5 * 2; // result is 20, multiplication first
    
  12. Operator Precedence and Associativity in Java:

    • Operators with the same precedence follow associativity rules (left-to-right or right-to-left). For example, addition and subtraction have the same precedence and associate left-to-right.
    int result = 10 - 5 + 2; // result is 7, left-to-right associativity
    
  13. Java Expression Evaluation Rules:

    • Expressions are evaluated based on operator precedence and associativity rules. Parentheses can be used to override default order.
    int result = (10 + 5) * 2; // result is 30, parentheses override
    
  14. Common Mistakes with Java Operator Precedence:

    • Mistakes often occur when assuming default left-to-right associativity. Parentheses can help clarify and avoid errors.
    int result = 10 + 5 * 2; // result is 20, multiplication first