Why care about Java operators?

Ever wonder how 2 + 3 becomes 5 in a program? That magic happens thanks to operators. They’re the tiny symbols that tell Java what to do with values.

💡 In Simple Words: An operator is a symbol that performs a calculation or comparison on numbers, characters or variables. Think of it as a math sign or a decision‑making tool that Java understands.

What are operators in Java?

In Java, an operator is a special character (or group of characters) that tells the computer to perform an action. Actions can be arithmetic (like addition), logical (like checking true/false), or even assigning a value to a variable.

Categories of Java operators

Java groups its operators into a few families. Each family has a purpose and a set of symbols you’ll see over and over.

CategoryOperatorsWhat they do
Arithmetic+, -, *, /, %Basic math – add, subtract, multiply, divide, remainder
Relational==, !=, >, =, Compare two values, give true/false
Logical&&, ||, !Combine or invert boolean (true/false) statements
Assignment=, +=, -=, *=, /=, %=Give a variable a value, optionally with an operation
Unary++, --, + (positive), - (negative)Change a variable by one or show sign

Arithmetic operators – the math toolbox

These are the ones you already know from school. Here’s a quick run‑through with code snippets.

  • + (addition): int sum = 7 + 5; // sum = 12
  • - (subtraction): int diff = 10 - 3; // diff = 7
  • * (multiplication): int prod = 4 * 6; // prod = 24
  • / (division): int quot = 20 / 4; // quot = 5
  • % (modulus): int rem = 22 % 5; // rem = 2 – it gives the remainder after division.

Remember: when you divide two int values, Java drops any decimal part. Use double if you need fractions.

Relational operators – the comparison crew

These operators compare two values and spit out a boolean – true or false. Think of them as the “yes/no” questions you ask a program.

  • == (equal to): 5 == 5 // true
  • != (not equal to): 5 != 3 // true
  • > (greater than): 7 > 2 // true
  • < (less than): 4 < 9 // true
  • >= (greater than or equal): 6 >= 6 // true
  • <= (less than or equal): 3 <= 8 // true

Use these in if statements to decide which block of code runs.

Logical operators – combining true/false

When you have more than one condition, logical operators help you join them.

  • && (AND): Both sides must be true. (a > 0) && (b < 10)
  • || (OR): At least one side true. (x == 5) || (y == 5)
  • ! (NOT): Flips true to false and vice‑versa. !isReady

Imagine you’re checking if a student passed both maths and science – you’d use AND. If you only need one subject passed, you’d use OR.

Assignment operators – giving values a home

The plain = just puts a value into a variable. The “compound” forms do the assignment *and* an operation in one go.

  • x = 5; – store 5 in x.
  • x += 3; – same as x = x + 3;. If x was 5, now it’s 8.
  • x -= 2; – subtract 2 then store.
  • x *= 4; – multiply by 4 then store.
  • x /= 2; – divide by 2 then store.
  • x %= 3; – remainder after division by 3.

These shortcuts keep code tidy and are a favorite on exams.

Unary operators – the one‑step tricks

Unary means “single operand”. The most common are the increment ++ and decrement --.

  • i++ – adds 1 to i *after* the current statement uses its old value (post‑increment).
  • ++i – adds 1 *before* the value is used (pre‑increment).
  • Same idea for i-- and --i.

Quick tip: In a loop like for(int i=0; i<5; i++), the i++ moves the counter forward each round.

Operator precedence & associativity – the order of operations

Just like math, Java decides which operator runs first. Multiplication and division happen before addition and subtraction. Parentheses ( ) let you override the default order.

Associativity tells Java what to do when operators of the same rank appear together. Most binary operators (those with two sides) are left‑to‑right.

Remember this simple rule: PEMDAS – Parentheses, Exponents (not in Java basics), Multiplication/Division, Addition/Subtraction. For logical operators, ! (NOT) comes first, then && (AND), then || (OR).

Quick cheat‑sheet – operator summary

  • Arithmetic: +, -, *, /, %
  • Relational: ==, !=, >, <, >=, <=
  • Logical: &&, ||, !
  • Assignment: =, +=, -=, *=, /=, %=
  • Unary: ++, --, + (positive), - (negative)

📝 Likely Exam Questions

  1. Write a Java statement that adds 10 to variable score using a compound assignment operator.
    Answer: score += 10;
  2. What will be the output of the following code?
    int a = 5; int b = 2; System.out.println(a / b);
    Answer: 2 (integer division discards the remainder).
  3. Explain the difference between i++ and ++i in a System.out.println(i++) statement.
    Answer: i++ prints the current value of i then increments; ++i would increment first then print the new value.
  4. Given int x = 7; int y = 3; evaluate boolean result = (x > y) && (x % y == 1);.
    Answer: true because both conditions are true.
  5. List three relational operators and give a short example for each.
    Answer: == (5 == 5), != (4 != 7), >= (9 >= 8).
#ICSE#Class 9#Computer Applications#Java#Operators