Operators and Expressions

Introduction to Operators and Expressions

Operators and expressions form the core of any programming language, including Java. They allow you to perform computations, make decisions, and manipulate data.

Types of Operators

Java provides several types of operators, each serving a specific purpose:

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

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations.

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulus (%)

Example: Arithmetic Operators

Java
public class ArithmeticExample {
    public static void main(String[] args) {
        int a = 10, b = 5;

        System.out.println("a + b = " + (a + b));  // 15
        System.out.println("a - b = " + (a - b));  // 5
        System.out.println("a * b = " + (a * b));  // 50
        System.out.println("a / b = " + (a / b));  // 2
        System.out.println("a % b = " + (a % b));  // 0
    }
}

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 (<=)

Example: Relational Operators

Java
public class RelationalExample {
    public static void main(String[] args) {
        int a = 10, b = 5;

        System.out.println("a == b: " + (a == b));  // false
        System.out.println("a != b: " + (a != b));  // true
        System.out.println("a > b: " + (a > b));    // true
        System.out.println("a < b: " + (a < b));    // false
        System.out.println("a >= b: " + (a >= b));  // true
        System.out.println("a <= b: " + (a <= b));  // false
    }
}

Logical Operators

Logical operators are used to perform logical operations, typically with boolean values.

  • AND (&&)
  • OR (||)
  • NOT (!)

Example: Logical Operators

Java
public class LogicalExample {
    public static void main(String[] args) {
        boolean x = true, y = false;

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

Bitwise Operators

Bitwise operators are used to perform bit-level operations on integer types.

  • AND (&)
  • OR (|)
  • XOR (^)
  • Complement (~)
  • Left shift (<<)
  • Right shift (>>)
  • Unsigned right shift (>>>)

Example: Bitwise Operators

Java
public class BitwiseExample {
    public static void main(String[] args) {
        int a = 5, b = 3;

        System.out.println("a & b: " + (a & b));  // 1
        System.out.println("a | b: " + (a | b));  // 7
        System.out.println("a ^ b: " + (a ^ b));  // 6
        System.out.println("~a: " + (~a));        // -6
        System.out.println("a << 1: " + (a << 1));  // 10
        System.out.println("a >> 1: " + (a >> 1));  // 2
        System.out.println("a >>> 1: " + (a >>> 1));  // 2
    }
}

Assignment Operators

Assignment operators are used to assign values to variables.

  • Assignment (=)
  • Addition assignment (+=)
  • Subtraction assignment (-=)
  • Multiplication assignment (*=)
  • Division assignment (/=)
  • Modulus assignment (%=)

Example: Assignment Operators

Java
public class AssignmentExample {
    public static void main(String[] args) {
        int a = 10;

        a += 5;  // a = a + 5
        System.out.println("a += 5: " + a);  // 15

        a -= 3;  // a = a - 3
        System.out.println("a -= 3: " + a);  // 12

        a *= 2;  // a = a * 2
        System.out.println("a *= 2: " + a);  // 24

        a /= 4;  // a = a / 4
        System.out.println("a /= 4: " + a);  // 6

        a %= 3;  // a = a % 3
        System.out.println("a %= 3: " + a);  // 0
    }
}

Unary Operators

Unary operators are used with only one operand.

  • Unary plus (+)
  • Unary minus (-)
  • Increment (++)
  • Decrement (--)
  • Logical complement (!)

Example: Unary Operators

Java
public class UnaryExample {
    public static void main(String[] args) {
        int a = 10;
        boolean b = false;

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

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

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

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

Ternary Operator

The ternary operator is a shorthand for if-else statements.

  • Ternary (? :)

Syntax

Java
result = (condition) ? value1 : value2;

Example: Ternary Operator

Java
public class TernaryExample {
    public static void main(String[] args) {
        int a = 10, b = 20;
        int max = (a > b) ? a : b;

        System.out.println("Max value: " + max);  // 20
    }
}

Combining Operators and Expressions

This example combines various operators and expressions in a single program.

Example: Combined Operators and Expressions

Java
public class CombinedExample {
    public static void main(String[] args) {
        int a = 10, b = 5;
        boolean result;

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

        // Relational and Logical
        result = (a > b) && (a != b);  // true
        System.out.println("(a > b) && (a != b): " + result);

        // Ternary
        String comparison = (a > b) ? "a is greater" : "b is greater";
        System.out.println("Ternary: " + comparison);

        // Bitwise
        int c = a & b;  // 5
        System.out.println("a & b: " + c);

        // Unary
        int d = ++a;  // 16
        System.out.println("++a: " + d);
    }
}

Explanation

  • Arithmetic and Assignment: a += b adds b to a and assigns the result to a.
  • Relational and Logical: (a > b) && (a != b) checks if a is greater than b and not equal to b.
  • Ternary: (a > b) ? "a is greater" : "b is greater" assigns a value based on the condition.
  • Bitwise: a & b performs a bitwise AND operation on a and b.
  • Unary: ++a increments a by 1 before assigning the value to d.

Conclusion

Operators and expressions are crucial for performing computations and controlling the flow of a program in Java. By understanding and using these operators effectively, you can write more efficient and readable code. Practice using different operators in various contexts to deepen your understanding and improve your programming skills.

Scroll to Top