Operators

Comprehensive study notes, diagrams, and exam preparation for Operators.

Operators

Definition

Operators are special symbols in Java that perform operations on one, two, or three operands and produce a result. An operand is a variable, literal, constant, or expression on which the operator acts.

In Java, operators are used to:

  • perform arithmetic calculations,
  • compare values,
  • assign values to variables,
  • control logical decisions,
  • manipulate bits,
  • and manage object or type-related operations.

Example:

int a = 10;
int b = 5;
int sum = a + b;

Here, + is an operator, and a and b are operands.

Java operators are a core part of programming because almost every meaningful instruction involves them in some way.


Main Content

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

  • + Addition
    Adds two values.
  int x = 10 + 5;   // 15
  • - Subtraction
    Subtracts one value from another.
  int x = 10 - 5;   // 5
  • * Multiplication
    Multiplies two values.
  int x = 10 * 5;   // 50
  • / Division
    Divides one value by another.
  int x = 10 / 5;   // 2

In integer division, the fractional part is discarded:

  int x = 7 / 2;    // 3
  • % Modulus
    Returns the remainder after division.
  int x = 7 % 2;    // 1

Important notes:

  • If both operands are integers, / gives integer quotient, not decimal result.
  • % is widely used in checking even/odd numbers, cyclic patterns, and remainder-based conditions.

Example:

int number = 14;
if (number % 2 == 0) {
    System.out.println("Even");
}

2. Relational and Logical Operators

These operators help compare values and make decisions.

Relational Operators

Relational operators compare two values and return true or false.

  • == Equal to
  5 == 5   // true
  • != Not equal to
  5 != 3   // true
  • > Greater than
  7 > 4   // true
  • < Less than
  2 < 6   // true
  • >= Greater than or equal to
  8 >= 8   // true
  • <= Less than or equal to
  3 <= 5   // true

Logical Operators

Logical operators combine multiple conditions.

  • && Logical AND
    Returns true only if both conditions are true.
  int age = 20;
  boolean hasID = true;
  if (age >= 18 && hasID) {
      System.out.println("Allowed");
  }
  • || Logical OR
    Returns true if at least one condition is true.
  if (age >= 18 || hasID) {
      System.out.println("Allowed");
  }
  • ! Logical NOT
    Reverses the boolean value.
  boolean result = !true;   // false

Short-circuit behavior:

  • && stops evaluating when the first condition is false.
  • || stops evaluating when the first condition is true.

This behavior improves efficiency and can prevent errors in conditions.


3. Assignment, Unary, Bitwise, and Ternary Operators

These operators are used for assigning values, changing the sign of variables, manipulating bits, and making compact decisions.

Assignment Operators

Assignment operators store or update values in variables.

  • = Simple assignment
  int a = 10;
  • += Add and assign
  a += 5;   // a = a + 5
  • -= Subtract and assign
  a -= 2;   // a = a - 2
  • *= Multiply and assign
  a *= 3;
  • /= Divide and assign
  a /= 2;
  • %= Modulus and assign
  a %= 4;

Unary Operators

Unary operators work on a single operand.

  • + Unary plus
    Indicates positive value.

  • - Unary minus
    Changes sign.

  int n = 5;
  int m = -n;   // -5
  • ++ Increment
    Increases value by 1.
  int i = 5;
  i++;   // 6
  ++i;   // 7
  • -- Decrement
    Decreases value by 1.
  int i = 5;
  i--;   // 4
  --i;   // 3

Prefix vs postfix:

  • Prefix: ++i changes the value first, then uses it.
  • Postfix: i++ uses the value first, then changes it.

Example:

int i = 5;
int a = ++i;  // i = 6, a = 6
int b = i++;  // b = 6, i becomes 7

Bitwise Operators

Bitwise operators work on individual bits of integer types.

  • & Bitwise AND
  • | Bitwise OR
  • ^ Bitwise XOR
  • ~ Bitwise NOT
  • << Left shift
  • >> Right shift
  • >>> Unsigned right shift

Example:

int x = 5;   // 0101
int y = 3;   // 0011
int z = x & y;  // 0001 = 1

Simple bit view:

5  = 0101
3  = 0011
&  = 0001

Bitwise operators are important in low-level programming, performance-sensitive code, and tasks involving flags, masks, and encoding.

Ternary Operator

The ternary operator is a compact form of if-else.

Syntax:

condition ? expression1 : expression2

Example:

int a = 10, b = 20;
int max = (a > b) ? a : b;

This means:

  • if a > b is true, max = a
  • otherwise, max = b

The ternary operator is useful when a simple conditional assignment is needed in one line.


Working / Process

  1. Identify the type of operation needed: mathematical, comparison, assignment, logical, bitwise, or conditional.
  2. Select the correct operator based on the desired result and the data type of the operands.
  3. Java evaluates the expression according to operator precedence and associativity, then produces the final value.

Example process:

int a = 10;
int b = 3;
int c = a + b * 2;

Here, multiplication happens before addition:

  • b * 2 = 6
  • a + 6 = 16

ASCII diagram for evaluation order:

a + b * 2
    |
    v
b * 2 = 6
    |
    v
a + 6 = 16

Operator precedence generally means:

  • parentheses first,
  • then unary operators,
  • then multiplication/division/modulus,
  • then addition/subtraction,
  • then relational,
  • then logical,
  • then assignment.

Example with precedence:

int result = 10 + 5 * 2 > 15 && true;

Evaluation:

  • 5 * 2 = 10
  • 10 + 10 = 20
  • 20 > 15 = true
  • true && true = true

Understanding this order is essential to write correct expressions.


Advantages / Applications

  • Operators make programs concise, readable, and efficient by reducing long instructions into compact expressions.
  • They are essential for decision-making, calculations, loop control, input validation, and algorithm implementation.
  • Operators are widely used in real applications such as grade calculation, banking systems, game logic, data processing, and bit-level optimization.

Examples of applications:

  • Arithmetic operators in billing and tax calculation.
  • Relational and logical operators in if, else, while, and for conditions.
  • Assignment operators in counters and accumulators.
  • Unary operators in iteration and indexing.
  • Bitwise operators in encryption, compression, hardware control, and permissions.
  • Ternary operator in compact selection logic.

Summary

  • Operators are symbols that perform actions on operands in Java.
  • Java provides arithmetic, relational, logical, assignment, unary, bitwise, and ternary operators.
  • Operator precedence and associativity control how expressions are evaluated.
  • Important terms to remember: operand, precedence, associativity, increment, decrement, logical operators, bitwise operators, ternary operator