Java Operators

Symbols that perform operations on variables and values

⚡ What are Java Operators?

Java operators are special symbols that perform specific operations on variables and values. They include arithmetic, comparison, logical, and assignment operators for calculations and decision-making.


// Basic operators in action
int a = 10, b = 5;
int sum = a + b;        // Addition
boolean isEqual = a == b; // Comparison
boolean result = true && false; // Logical

System.out.println("Sum: " + sum);
System.out.println("Equal: " + isEqual);
                                    

Output:

Sum: 15

Equal: false

Types of Java Operators

Arithmetic

Mathematical operations

int result = 10 + 5;  // Addition
int diff = 10 - 5;    // Subtraction
⚖️

Comparison

Compare values

boolean isGreater = 10 > 5;  // true
boolean isEqual = 10 == 5;   // false
🧠

Logical

Combine boolean values

boolean and = true && false; // false
boolean or = true || false;  // true
📝

Assignment

Assign values to variables

int x = 10;    // Simple assignment
x += 5;        // Add and assign

🔹 Arithmetic Operators

Perform mathematical calculations:

int a = 20, b = 6;

// Basic arithmetic
int addition = a + b;       // 26
int subtraction = a - b;    // 14
int multiplication = a * b; // 120
int division = a / b;       // 3 (integer division)
int remainder = a % b;      // 2 (modulus)

// Increment and decrement
int counter = 5;
counter++;  // Increment by 1 (now 6)
counter--;  // Decrement by 1 (now 5)

System.out.println("Addition: " + addition);
System.out.println("Division: " + division);
System.out.println("Remainder: " + remainder);

Output:

Addition: 26

Division: 3

Remainder: 2

🔹 Comparison Operators

Compare values and return true or false:

int x = 10, y = 20;

// Comparison operators
boolean equal = (x == y);        // false
boolean notEqual = (x != y);     // true
boolean greater = (x > y);       // false
boolean less = (x < y);          // true
boolean greaterEqual = (x >= 10); // true
boolean lessEqual = (y <= 20);   // true

System.out.println("x equals y: " + equal);
System.out.println("x less than y: " + less);
System.out.println("x >= 10: " + greaterEqual);

Output:

x equals y: false

x less than y: true

x >= 10: true

🔹 Logical Operators

Combine or modify boolean values:

boolean isAdult = true;
boolean hasLicense = false;
boolean isWeekend = true;

// Logical AND (&&) - both must be true
boolean canDrive = isAdult && hasLicense;  // false

// Logical OR (||) - at least one must be true
boolean canRelax = isWeekend || !hasLicense; // true

// Logical NOT (!) - reverses the boolean
boolean isChild = !isAdult;  // false

System.out.println("Can drive: " + canDrive);
System.out.println("Can relax: " + canRelax);
System.out.println("Is child: " + isChild);

Output:

Can drive: false

Can relax: true

Is child: false

🔹 Assignment Operators

Assign values and perform operations:

int score = 100;

// Compound assignment operators
score += 10;  // score = score + 10 (now 110)
score -= 5;   // score = score - 5  (now 105)
score *= 2;   // score = score * 2  (now 210)
score /= 3;   // score = score / 3  (now 70)
score %= 8;   // score = score % 8  (now 6)

System.out.println("Final score: " + score);

// Multiple assignments
int a, b, c;
a = b = c = 50;  // All variables get value 50
System.out.println("a=" + a + ", b=" + b + ", c=" + c);

Output:

Final score: 6

a=50, b=50, c=50

🔹 Operator Precedence

Order in which operators are evaluated:

// Without parentheses
int result1 = 10 + 5 * 2;  // 20 (multiplication first)

// With parentheses
int result2 = (10 + 5) * 2;  // 30 (addition first)

// Complex expression
int a = 5, b = 10, c = 15;
int result3 = a + b * c / 5 - 2;  // 5 + (10*15)/5 - 2 = 33

System.out.println("Result 1: " + result1);
System.out.println("Result 2: " + result2);
System.out.println("Result 3: " + result3);

Output:

Result 1: 20

Result 2: 30

Result 3: 33

Operator Precedence (High to Low):

  1. Parentheses ( )
  2. Multiplication *, Division /, Modulus %
  3. Addition +, Subtraction -
  4. Comparison <, >, <=, >=
  5. Equality ==, !=
  6. Logical AND &&
  7. Logical OR ||
  8. Assignment =, +=, -=, etc.

🧠 Test Your Knowledge

What is the result of: 10 + 5 * 2?