⬅ Previous Next ➡

Operators in Java

Java Operators: Arithmetic, Relational, Logical, Assignment, Unary, Bitwise, Ternary & Precedence
  • Operators are symbols used to perform operations on variables and values in Java.
  • This chapter covers: Arithmetic, Relational, Logical, Assignment, Unary, Bitwise, Ternary operators and operator precedence.

1) Arithmetic Operators

  • Used for mathematical calculations.
  • + (add), - (subtract), * (multiply), / (divide), % (modulus/remainder)
  • Note: if both operands are int, / gives integer division.
class ArithmeticOps {
    public static void main(String[] args) {
        int a = 10, b = 3;

        System.out.println(a + b); // 13
        System.out.println(a - b); // 7
        System.out.println(a * b); // 30
        System.out.println(a / b); // 3  (int division)
        System.out.println(a % b); // 1
    }
}

2) Relational (Comparison) Operators

  • Used to compare values; result is true or false.
  • == equal to, != not equal, > greater than, = greater/equal, 15 n -= 2; // 13 n *= 3; // 39 n /= 3; // 13 n %= 5; // 3 System.out.println(n); } }

    5) Unary Operators

    • Operate on a single operand.
    • + unary plus, - unary minus, ++ increment, -- decrement, ! logical NOT, ~ bitwise NOT
    • Pre: ++a changes then uses; Post: a++ uses then changes.
    class UnaryOps {
        public static void main(String[] args) {
            int a = 5;
    
            System.out.println(++a); // 6 (pre-increment)
            System.out.println(a++); // 6 (post-increment, then a becomes 7)
            System.out.println(a);   // 7
    
            boolean ok = false;
            System.out.println(!ok); // true
        }
    }

    6) Bitwise Operators

    • Work on bits of integer types (int, long, etc.).
    • & AND, | OR, ^ XOR, ~ NOT
    • << left shift, >> right shift (sign), >>> unsigned right shift
    class BitwiseOps {
        public static void main(String[] args) {
            int a = 5;  // 0101
            int b = 3;  // 0011
    
            System.out.println(a & b);  // 1  (0001)
            System.out.println(a | b);  // 7  (0111)
            System.out.println(a ^ b);  // 6  (0110)
            System.out.println(~a);     // bitwise NOT
    
            System.out.println(a  1010)
            System.out.println(a >> 1); // 2
        }
    }

    7) Ternary Operator

    • Short form of if-else.
    • Syntax: condition ? value_if_true : value_if_false
    class TernaryOp {
        public static void main(String[] args) {
            int marks = 72;
    
            String result = (marks >= 40) ? "Pass" : "Fail";
            System.out.println(result);
    
            int a = 10, b = 20;
            int max = (a > b) ? a : b;
            System.out.println("Max: " + max);
        }
    }

    8) Operator Precedence (High to Low - Common)

    • () parentheses
    • Unary: ++, --, +, -, !, ~
    • Multiplicative: *, /, %
    • Additive: +, -
    • Shift: <<, >>, >>>
    • Relational: <, >, <=, >=
    • Equality: ==, !=
    • Bitwise: &, ^, |
    • Logical: &&, ||
    • Ternary: ? :
    • Assignment: =, +=, -=, *=, /=, ...
    • Use parentheses () when in doubt to make expressions clear.
    class PrecedenceDemo {
        public static void main(String[] args) {
            int a = 10, b = 5, c = 2;
    
            int ans1 = a + b * c;        // * first => 10 + (5*2) = 20
            int ans2 = (a + b) * c;      // ( ) first => (10+5)*2 = 30
    
            boolean ans3 = a > b && b > c; // > first, then &&
    
            System.out.println(ans1);
            System.out.println(ans2);
            System.out.println(ans3);
        }
    }