Java Operators - Complete Guide
Introduction to Java Operators
Operators in Java are special symbols that perform operations on operands and return a result.
They are the foundation of expressions and are grouped into categories: arithmetic, assignment, comparison, logical, bitwise, ternary, and more.
Arithmetic Operators
Arithmetic operators handle basic math operations like addition, subtraction, multiplication, division, and modulus. Java also supports increment and decrement operators.
public class ArithmeticOperators {
public static void main(String[] args) {
int a = 10, b = 3;
System.out.println("a + b = " + (a + b));
System.out.println("a - b = " + (a - b));
System.out.println("a * b = " + (a * b));
System.out.println("a / b = " + (a / b));
System.out.println("a % b = " + (a % b));
int c = 5;
System.out.println("c++ = " + (c++)); // Post-increment
System.out.println("++c = " + (++c)); // Pre-increment
int d = 8;
System.out.println("d-- = " + (d--)); // Post-decrement
System.out.println("--d = " + (--d)); // Pre-decrement
}
}
a + b = 13 a - b = 7 a * b = 30 a / b = 3 a % b = 1 c++ = 5 ++c = 7 d-- = 8 --d = 6
Assignment and Comparison Operators
Assignment operators assign values, often in shorthand form. Comparison operators evaluate relationships between values, returning boolean results.
public class AssignmentComparison {
public static void main(String[] args) {
int x = 10;
x += 5;
x -= 3;
x *= 2;
x /= 4;
x %= 3;
System.out.println("x after operations: " + x);
int a = 5, b = 8;
System.out.println("a == b: " + (a == b));
System.out.println("a != b: " + (a != b));
System.out.println("a > b: " + (a > b));
System.out.println("a < b: " + (a < b));
System.out.println("a >= b: " + (a >= b));
System.out.println("a <= b: " + (a <= b));
}
}
x after operations: 0 a == b: false a != b: true a > b: false a < b: true a >= b: false a <= b: true
Logical and Bitwise Operators
Logical operators combine boolean expressions, while bitwise operators manipulate individual bits of integers.
public class LogicalBitwise {
public static void main(String[] args) {
boolean p = true, q = false;
System.out.println("p && q: " + (p && q));
System.out.println("p || q: " + (p || q));
System.out.println("!p: " + (!p));
int x = 5; // 0101
int y = 3; // 0011
System.out.println("x & y: " + (x & y));
System.out.println("x | y: " + (x | y));
System.out.println("x ^ y: " + (x ^ y));
System.out.println("~x: " + (~x));
System.out.println("x << 1: " + (x << 1));
System.out.println("x >> 1: " + (x >> 1));
System.out.println("x >>> 1: " + (x >>> 1));
}
}
p && q: false p || q: true !p: false x & y: 1 x | y: 7 x ^ y: 6 ~x: -6 x << 1: 10 x >> 1: 2 x >>> 1: 2
Ternary and instanceof Operators
The ternary operator `?:` is a compact if-else. The `instanceof` operator checks if an object belongs to a particular class.
public class TernaryInstanceof {
public static void main(String[] args) {
int age = 20;
String status = (age >= 18) ? "Adult" : "Minor";
System.out.println("Status: " + status);
int score = 85;
String grade = (score >= 90) ? "A" : (score >= 80) ? "B" : "C";
System.out.println("Grade: " + grade);
Object obj = "Hello";
System.out.println("obj instanceof String: " + (obj instanceof String));
System.out.println("obj instanceof Object: " + (obj instanceof Object));
}
}
Status: Adult Grade: B obj instanceof String: true obj instanceof Object: true
Operator Precedence and Best Practices
Operator precedence controls evaluation order. Using parentheses improves readability and prevents logical errors.
- ✅ Always use parentheses for clarity in complex expressions
- ✅ Remember that integer division truncates results
- ✅ Prefer `&&` and `||` for short-circuiting instead of `&` and `|`
- ✅ Keep ternary usage simple; avoid nested ternaries for readability
- ✅ Use compound assignments like `x += 5` to simplify expressions
Precedence | Operator | Description | Associativity |
---|---|---|---|
Highest | () [] . | Parentheses, array access, member access | Left to right |
++ -- ! ~ | Unary operators | Right to left | |
* / % | Multiplicative | Left to right | |
+ - | Additive | Left to right | |
<< >> >>> | Shift | Left to right | |
< <= > >= instanceof | Relational | Left to right | |
== != | Equality | Left to right | |
& | Bitwise AND | Left to right | |
^ | Bitwise XOR | Left to right | |
| | Bitwise OR | Left to right | |
&& | Logical AND | Left to right | |
|| | Logical OR | Left to right | |
?: | Ternary | Right to left | |
Lowest | = += -= *= /= %= | Assignment | Right to left |