1. Introduction
Operators in Java are special symbols that perform operations on variables and values, called operands. Java provides a rich set of operators for arithmetic, comparison, logical decision-making, bit manipulation, assignment, and type checking.
Cricket analogy: Just as a scorer uses distinct symbols—runs, wickets, extras, boundaries—to record every ball bowled, Java uses distinct operator symbols for arithmetic, comparison, logical, bitwise, assignment, and type-checking operations on its operands.
Understanding operator categories and their precedence (the order in which operators are evaluated in an expression) is essential for writing correct expressions and is a frequently tested exam topic.
Cricket analogy: Just as a batting order determines who faces the bowler first regardless of who scored more runs last match, operator precedence determines which operation in an expression is evaluated first, a rule every Java exam tests closely.
2. Syntax
2.1 Arithmetic and Unary Operators
int a = 10, b = 3;
int sum = a + b; // 13
int diff = a - b; // 7
int prod = a * b; // 30
int quot = a / b; // 3 (integer division)
int mod = a % b; // 1 (remainder)
int x = 5;
x++; // post-increment, unary
--x; // pre-decrement, unary2.2 Relational and Logical Operators
boolean r1 = (a > b); // relational: > < >= <= == !=
boolean r2 = (a > b) && (b > 0); // logical AND
boolean r3 = (a > b) || (b < 0); // logical OR
boolean r4 = !(a > b); // logical NOT2.3 Bitwise, Assignment, Ternary, instanceof
int bAnd = a & b; // bitwise AND
int bOr = a | b; // bitwise OR
int bXor = a ^ b; // bitwise XOR
int bNot = ~a; // bitwise complement
int left = a << 1; // left shift
int right = a >> 1; // right shift
int c = 5;
c += 2; // assignment operator, same as c = c + 2
int max = (a > b) ? a : b; // ternary operator
Object obj = "hello";
boolean isString = obj instanceof String; // instanceof operator3. Explanation
Arithmetic operators (+, -, *, /, %) perform mathematical calculations. Note that integer division truncates the decimal part, and % returns the remainder of division. Relational operators (>, <, >=, <=, ==, !=) compare two values and return a boolean. Logical operators (&&, ||, !) combine boolean expressions; && and || use short-circuit evaluation, meaning the second operand is not evaluated if the result is already determined by the first.
Cricket analogy: Integer division truncating decimals is like a run-rate calculator dropping fractional overs, while % is the leftover balls in an over; relational operators like runs > 50 return true/false for a half-century, and && short-circuits if the first condition (wicket fell) already decides the over is done.
Bitwise operators (&, |, ^, ~, <<, >>, >>>) operate at the bit level on integer types. Assignment operators (=, +=, -=, *=, /=, %=) assign or compute-and-assign values. Unary operators (+, -, ++, --, !) act on a single operand — note the difference between pre-increment (++x, increments then uses value) and post-increment (x++, uses value then increments). The ternary operator (condition ? valueIfTrue : valueIfFalse) is a compact one-line if-else. The instanceof operator tests whether an object is an instance of a specific class or interface, returning a boolean.
Cricket analogy: Bitwise operators are like fine-grained field placement tweaks bit by bit, runs += 4 is an assignment operator adding a boundary, pre-increment ++overs updates the over count before the ball is bowled while post-increment overs++ uses the old count first, the ternary picks 'Win' or 'Loss' in one line, and instanceof checks if a player object is actually a Bowler.
Operator precedence determines evaluation order when multiple operators appear in one expression: postfix (++/--) and unary operators bind tightest, followed by multiplicative (* / %), additive (+ -), shift, relational, equality, bitwise AND/XOR/OR, logical AND (&&), logical OR (||), ternary (?:), and finally assignment (=) which has the lowest precedence. Parentheses can always be used to override default precedence and improve readability.
Cricket analogy: Just as a batting lineup fixes who bats first (openers) down to who bats last (tail-enders) unless the captain overrides the order, Java fixes unary and postfix operators to bind tightest down to assignment binding loosest, with parentheses acting as the captain's override.
Short-circuit evaluation: in (a > b) && (b/0 > 1), if a > b is false, the second operand is never evaluated, avoiding a potential ArithmeticException. This is a common trick question in exams.
4. Example
public class OperatorsDemo {
public static void main(String[] args) {
int a = 10, b = 3;
System.out.println("Sum: " + (a + b));
System.out.println("Modulus: " + (a % b));
boolean isGreater = a > b;
System.out.println("isGreater: " + isGreater);
boolean logical = (a > b) && (b > 0);
System.out.println("Logical AND: " + logical);
int max = (a > b) ? a : b;
System.out.println("Max (ternary): " + max);
Object obj = "Java";
System.out.println("instanceof String: " + (obj instanceof String));
int x = 5;
System.out.println("Post-increment: " + (x++));
System.out.println("After post-increment: " + x);
}
}5. Output
Sum: 13
Modulus: 1
isGreater: true
Logical AND: true
Max (ternary): 10
instanceof String: true
Post-increment: 5
After post-increment: 66. Key Takeaways
- Java supports arithmetic, relational, logical, bitwise, assignment, unary, ternary, and instanceof operators.
- && and || use short-circuit evaluation; & and | do not.
- Pre-increment (++x) updates before use; post-increment (x++) updates after use.
- The ternary operator (?:) is a compact substitute for simple if-else statements.
- instanceof checks if an object is of a given type and returns a boolean.
- Operator precedence controls evaluation order; use parentheses to make intent explicit.
Practice what you learned
1. What is the result of 10 % 3 in Java?
2. Which logical operator performs short-circuit evaluation?
3. What is the output of: int x = 5; System.out.println(x++);
4. Which operator checks whether an object is an instance of a particular class?
5. What does the ternary operator (condition ? a : b) do?
Was this page helpful?
You May Also Like
Type Casting in Java
Understand widening (implicit) and narrowing (explicit) type casting in Java, plus autoboxing and unboxing between primitives and wrapper classes.
if-else in Java
Learn how Java's if-else statement controls program flow with conditional branching, including if, if-else, else-if ladders, and nested if statements.
Variables in Java
Learn what variables are in Java, how to declare and initialize them, naming rules, and the difference between local, instance, and static variables.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics