Operators perform operations on variables and values. In test automation, you use arithmetic to calculate expected values, comparison to assert results, and logical operators to combine conditions.
public class Arithmetic {
public static void main(String[] args) {
int a = 17, b = 5;
System.out.println("a + b = " + (a + b)); // 22
System.out.println("a - b = " + (a - b)); // 12
System.out.println("a * b = " + (a * b)); // 85
System.out.println("a / b = " + (a / b)); // 3 (integer division)
System.out.println("a % b = " + (a % b)); // 2 (remainder)
// Practical use: check if a number is even or odd
int testCases = 7;
System.out.println("Even? " + (testCases % 2 == 0)); // false
}
}public class IncrementDemo {
public static void main(String[] args) {
int count = 0;
count++; // count is now 1
count++; // count is now 2
count--; // count is now 1
// Pre vs Post increment
int x = 5;
System.out.println(x++); // prints 5, THEN increments x to 6
System.out.println(x); // prints 6
int y = 5;
System.out.println(++y); // increments y to 6, THEN prints 6
System.out.println(y); // prints 6
}
}Comparison operators return a boolean (true or false). Every assertion in test automation ultimately relies on a comparison.
public class Comparison {
public static void main(String[] args) {
int expected = 10;
int actual = 10;
System.out.println(actual == expected); // true — equal
System.out.println(actual != expected); // false — not equal
System.out.println(actual > 5); // true
System.out.println(actual < 5); // false
System.out.println(actual >= 10); // true
System.out.println(actual <= 9); // false
}
}== compares primitive values. For Strings and objects, == compares memory references, not content. Use .equals() for String comparison: str1.equals(str2).
Logical operators combine multiple boolean expressions. In automation, you use them for complex conditions: "element is visible AND text matches" or "login failed OR timeout occurred".
public class Logical {
public static void main(String[] args) {
boolean isVisible = true;
boolean isEnabled = true;
boolean hasError = false;
// AND — both must be true
System.out.println(isVisible && isEnabled); // true
System.out.println(isVisible && hasError); // false
// OR — at least one must be true
System.out.println(isVisible || hasError); // true
System.out.println(false || hasError); // false
// NOT — inverts
System.out.println(!hasError); // true
System.out.println(!isVisible); // false
// Combined
int age = 25;
boolean hasLicense = true;
boolean canDrive = age >= 18 && hasLicense;
System.out.println("Can drive: " + canDrive); // true
}
}public class Assignment {
public static void main(String[] args) {
int total = 100;
total += 20; // total = total + 20 → 120
total -= 30; // total = total - 30 → 90
total *= 2; // total = total * 2 → 180
total /= 3; // total = total / 3 → 60
total %= 7; // total = total % 7 → 4
System.out.println(total); // 4
}
}When multiple operators appear in one expression, Java follows precedence rules. Use parentheses to make intent clear.
| Priority | Operators | Example |
|---|---|---|
| 1 (highest) | () parentheses | (a + b) * c |
| 2 | ++ -- ! (unary) | !isValid, count++ |
| 3 | * / % | 10 * 3 / 2 |
| 4 | + - | 5 + 3 - 1 |
| 5 | < > <= >= == != | a > b |
| 6 | && (AND) | a && b |
| 7 | || (OR) | a || b |
| 8 (lowest) | = += -= *= /= | x = 5 |
public class Precedence {
public static void main(String[] args) {
int result = 2 + 3 * 4; // 14, not 20 — multiplication first
int result2 = (2 + 3) * 4; // 20 — parentheses override
boolean check = 5 > 3 && 10 < 20 || false;
// Step 1: 5 > 3 → true
// Step 2: 10 < 20 → true
// Step 3: true && true → true
// Step 4: true || false → true
System.out.println(check); // true
}
}Q: What is the difference between == and .equals() in Java?
A: == compares memory references (whether two variables point to the same object in memory). .equals() compares the actual content/value. For primitives, == compares values directly. For objects like Strings, == can return false even when the content is identical if they are different objects in memory. Always use .equals() for String comparison.
Q: What is short-circuit evaluation?
A: In && (AND), if the left side is false, Java skips the right side — the result is already false. In || (OR), if the left side is true, Java skips the right side — the result is already true. This is called short-circuit evaluation. It is useful for null checks: str != null && str.length() > 0 prevents NullPointerException because if str is null, the second condition is never evaluated.
Exercise 1: Write a program that calculates the total price of 3 items with a 15% discount, then adds 8% tax on the discounted price. Print the breakdown.
Exercise 2: Given int x = 5;, what is the value of x after each line? Predict first:
``
x += 3;
x *= 2;
x -= 4;
x %= 3;
``
Exercise 3: Write boolean expressions that check: (a) whether a number is between 1 and 100 inclusive, (b) whether a String is not null and not empty.