Operators are special symbols in Java that perform specific operations on one, two, or three operands, and then return a result. Java has various types of operators which can be categorized as follows:

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

1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations.

  • + Addition
  • - Subtraction
  • * Multiplication
  • / Division
  • % Modulus
  public class Main {
    public static void main(String[] args) {
        int a = 10;
        int b = 5;

        System.out.println("Addition: " + (a + b));       // 15
        System.out.println("Subtraction: " + (a - b));    // 5
        System.out.println("Multiplication: " + (a * b)); // 50
        System.out.println("Division: " + (a / b));       // 2
        System.out.println("Modulus: " + (a % b));        // 0
    }
}
  

2. Relational Operators

Relational operators are used to compare two values.

  • == Equal to
  • != Not equal to
  • > Greater than
  • < Less than
  • >= Greater than or equal to
  • <= Less than or equal to
  public class Main {
    public static void main(String[] args) {
        int a = 10;
        int b = 5;

        System.out.println("Equal to: " + (a == b));        // false
        System.out.println("Not equal to: " + (a != b));    // true
        System.out.println("Greater than: " + (a > b));     // true
        System.out.println("Less than: " + (a < b));        // false
        System.out.println("Greater or equal: " + (a >= b));// true
        System.out.println("Less or equal: " + (a <= b));   // false
    }
}
  

3. Logical Operators

Logical operators are used to combine multiple boolean expressions.

  • && Logical AND
  • || Logical OR
  • ! Logical NOT
  public class Main {
    public static void main(String[] args) {
        boolean x = true;
        boolean y = false;

        System.out.println("Logical AND: " + (x && y)); // false
        System.out.println("Logical OR: " + (x || y));  // true
        System.out.println("Logical NOT: " + (!x));     // false
    }
}
  

4. Bitwise Operators

Bitwise operators are used to perform bit-level operations.

  • & Bitwise AND
  • | Bitwise OR
  • ^ Bitwise XOR
  • ~ Bitwise Complement
  • << Left Shift
  • >> Right Shift
  • >>> Unsigned Right Shift
  public class Main {
    public static void main(String[] args) {
        int a = 5;  // 0101 in binary
        int b = 3;  // 0011 in binary

        System.out.println("Bitwise AND: " + (a & b)); // 0001 -> 1
        System.out.println("Bitwise OR: " + (a | b));  // 0111 -> 7
        System.out.println("Bitwise XOR: " + (a ^ b)); // 0110 -> 6
        System.out.println("Bitwise NOT: " + (~a));    // 1010 -> -6
        System.out.println("Left Shift: " + (a << 1)); // 1010 -> 10
        System.out.println("Right Shift: " + (a >> 1));// 0010 -> 2
        System.out.println("Unsigned Right Shift: " + (a >>> 1)); // 0010 -> 2
    }
}
  

5. Assignment Operators

Assignment operators are used to assign values to variables.

  • = Assign
  • += Add and assign
  • -= Subtract and assign
  • *= Multiply and assign
  • /= Divide and assign
  • %= Modulus and assign
  public class Main {
    public static void main(String[] args) {
        int a = 10;
        int b = 5;

        a += b; // a = a + b
        System.out.println("Add and assign: " + a); // 15

        a -= b; // a = a - b
        System.out.println("Subtract and assign: " + a); // 10

        a *= b; // a = a * b
        System.out.println("Multiply and assign: " + a); // 50

        a /= b; // a = a / b
        System.out.println("Divide and assign: " + a); // 10

        a %= b; // a = a % b
        System.out.println("Modulus and assign: " + a); // 0
    }
}
  

6. Unary Operators

Unary operators are used with only one operand.

  • + Unary plus
  • - Unary minus
  • ++ Increment
  • -- Decrement
  • ! Logical complement
  public class Main {
    public static void main(String[] args) {
        int a = 10;

        System.out.println("Unary plus: " + (+a));  // 10
        System.out.println("Unary minus: " + (-a)); // -10

        a++; // a = a + 1
        System.out.println("Increment: " + a); // 11

        a--; // a = a - 1
        System.out.println("Decrement: " + a); // 10

        boolean b = false;
        System.out.println("Logical complement: " + (!b)); // true
    }
}
  

7. Ternary Operator

The ternary operator is a shorthand for the if-else statement.

  • ? : Conditional
  public class Main {
    public static void main(String[] args) {
        int a = 10;
        int b = 5;

        String result = (a > b) ? "a is greater than b" : "a is not greater than b";
        System.out.println(result); // a is greater than b
    }
}