Lesson Completion
Back to course

Module 03: Operators and Expressions

Overview

This module provides in-depth coverage of all Java operators, operator precedence, expressions, and their practical applications in programming.

Learning Objectives

  • Master all types of operators in Java
  • Understand operator precedence and associativity
  • Learn to write complex expressions
  • Work with bitwise and logical operations
  • Understand short-circuit evaluation
  • Apply operators in real-world scenarios

Topics Covered

3.1 Arithmetic Operators

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulus (%)
  • Unary Plus (+)
  • Unary Minus (-)
  • Increment (++)
    • Pre-increment
    • Post-increment
  • Decrement (--)
    • Pre-decrement
    • Post-decrement

3.2 Assignment Operators

  • Simple Assignment (=)
  • Compound Assignment Operators
    • += (Add and assign)
    • -= (Subtract and assign)
    • *= (Multiply and assign)
    • /= (Divide and assign)
    • %= (Modulus and assign)
    • &= (Bitwise AND and assign)
    • |= (Bitwise OR and assign)
    • ^= (Bitwise XOR and assign)
    • <<= (Left shift and assign)
    • = (Right shift and assign)

    • = (Unsigned right shift and assign)

3.3 Relational Operators

  • Equal to (==)
  • Not equal to (!=)
  • Greater than (>)
  • Less than (<)
  • Greater than or equal to (>=)
  • Less than or equal to (<=)
  • Comparing Objects vs Primitives

3.4 Logical Operators

  • Logical AND (&&)
  • Logical OR (||)
  • Logical NOT (!)
  • Short-Circuit Evaluation
  • Non-Short-Circuit Operators (&, |)
  • Truth Tables
  • De Morgan's Laws

3.5 Bitwise Operators

  • Bitwise AND (&)
  • Bitwise OR (|)
  • Bitwise XOR (^)
  • Bitwise Complement (~)
  • Left Shift (<<)
  • Right Shift (>>)
  • Unsigned Right Shift (>>>)
  • Practical Applications
  • Bit Manipulation Tricks

3.6 Ternary Operator

  • Conditional Operator (?:)
  • Syntax and Usage
  • Nested Ternary Operators
  • Alternatives to if-else
  • Best Practices

3.7 instanceof Operator

  • Type Comparison
  • Usage with Objects
  • Polymorphism and instanceof
  • Pattern Matching (Java 14+)

3.8 Operator Precedence and Associativity

  • Precedence Rules
  • Associativity (Left-to-Right, Right-to-Left)
  • Using Parentheses
  • Expression Evaluation Order
  • Precedence Table

3.9 Type Comparison Operators

  • == vs equals()
  • Comparing Strings
  • Comparing Objects
  • null Comparisons
  • Common Pitfalls

3.10 Expressions

  • Simple Expressions
  • Compound Expressions
  • Mixed-Type Expressions
  • Expression Statements
  • Side Effects in Expressions

3.11 Mathematical Operations

  • Math Class Methods
  • Common Mathematical Functions
  • Random Number Generation
  • Rounding Operations
  • Constants (PI, E)

3.12 String Concatenation

  • Using + Operator
  • String Builder vs Concatenation
  • Performance Considerations
  • Concatenation with Other Types

Hands-on Exercises

  1. Write programs using all arithmetic operators
  2. Implement a calculator using different operators
  3. Practice bitwise operations (set, clear, toggle bits)
  4. Create logical expression puzzles
  5. Demonstrate operator precedence with examples
  6. Write programs using ternary operators
  7. Implement bit manipulation algorithms
  8. Compare performance of different operators

Key Takeaways

  • Java provides rich set of operators
  • Operator precedence determines evaluation order
  • Short-circuit evaluation improves performance
  • Bitwise operators work at bit level
  • Proper use of parentheses improves readability
  • Understanding operators is fundamental to programming

Common Mistakes to Avoid

  • Confusing = with ==
  • Incorrect operator precedence assumptions
  • Using bitwise operators instead of logical operators
  • Integer division truncation issues
  • Overflow in arithmetic operations
  • Comparing objects with ==

Real-World Applications

  • Mathematical calculations
  • Bit flags and permissions
  • Performance optimizations
  • Conditional logic
  • Data validation

Additional Resources

  • Java Language Specification - Operators
  • Effective Java - Best Practices
  • Bit Twiddling Hacks
  • Operator Precedence Reference

Assessment

  • Quiz on operator types and precedence
  • Practical: Build expression evaluator
  • Debug exercises with operator errors
  • Bitwise manipulation challenges

Previous Module

Module 02: Java Basics

Next Module

Module 04: Control Flow