Operators in Java – Types and Precedence with Examples
An operator is a symbol that tells Java to perform an operation on one or more values, called operands. In price * quantity, the operator * multiplies the operands price and quantity. Operators are how Java calculates, compares, makes decisions and changes variables.
This overview introduces every category of Java operator with a quick example, then explains precedence: the rules that decide which operator runs first. Each category also has its own detailed tutorial.
1. What Is an Operator?
An operator plus its operands forms an expression that produces a value. Operators are classified by the number of operands they take:
- Unary operators take one operand:
-x,!done,count++. - Binary operators take two:
a + b,x > y. - Ternary operator takes three:
condition ? a : b.
2. Types of Operators
| Category | Operators | Example |
|---|---|---|
| Arithmetic | + - * / % | total = price * qty; |
| Unary | + - ++ -- ! ~ | count++; |
| Assignment | = += -= *= /= %= &= |= ^= <<= >>= >>>= | score += 10; |
| Relational | == != > < >= <= | age >= 18 |
| Logical | && || ! | isMember && hasCoupon |
| Bitwise and shift | & | ^ ~ << >> >>> | flags & MASK |
| Ternary | ? : | max = a > b ? a : b; |
| Type comparison | instanceof | obj instanceof String s |
| Other | . (member access), [] (array index), () (call/cast), -> (lambda), :: (method reference), new | list.get(0) |
3. All Categories in One Program
public class OperatorTour {
public static void main(String[] args) {
int a = 17, b = 5;
// Arithmetic
System.out.println("a + b = " + (a + b) + ", a - b = " + (a - b) + ", a * b = " + (a * b)
+ ", a / b = " + (a / b) + ", a % b = " + (a % b));
// Unary
int c = a;
System.out.println("-c = " + (-c) + ", ++c = " + (++c) + ", !true = " + !true);
// Assignment
int total = 100;
total += 20;
total *= 2;
System.out.println("total = " + total);
// Relational
System.out.println("a > b: " + (a > b) + ", a == b: " + (a == b) + ", a != b: " + (a != b));
// Logical
boolean hasTicket = true, isAdult = false;
System.out.println("hasTicket && isAdult: " + (hasTicket && isAdult) + ", hasTicket || isAdult: " + (hasTicket || isAdult));
// Bitwise and shift
System.out.println("6 & 3 = " + (6 & 3) + ", 6 | 3 = " + (6 | 3) + ", 6 ^ 3 = " + (6 ^ 3) + ", 1 << 4 = " + (1 << 4));
// Ternary
String result = a % 2 == 0 ? "even" : "odd";
System.out.println(a + " is " + result);
// instanceof
Object o = "text";
System.out.println("o instanceof String: " + (o instanceof String));
}
}
a + b = 22, a - b = 12, a * b = 85, a / b = 3, a % b = 2 -c = -17, ++c = 18, !true = false total = 240 a > b: true, a == b: false, a != b: true hasTicket && isAdult: false, hasTicket || isAdult: true 6 & 3 = 2, 6 | 3 = 7, 6 ^ 3 = 5, 1 << 4 = 16 17 is odd o instanceof String: true
Notice that 17 / 5 is 3: dividing two integers drops the remainder, which % returns.
4. Arithmetic Operators
+, -, *, / and % work on numbers. + also joins strings. Integer division truncates, and dividing an integer by zero throws ArithmeticException, while floating-point division by zero gives Infinity.
5. Unary Operators
Unary operators act on one value: - negates, ++ and -- add or subtract one, ! flips a boolean, and ~ flips all bits. The position of ++ matters: ++x changes x before its value is used, x++ after.
6. Assignment Operators
= stores a value. Compound operators such as += combine an operation with assignment, and they include an automatic cast: byte b = 10; b += 5; compiles, while b = b + 5; does not.
7. Relational Operators
Relational operators compare values and return true or false. For objects, == compares references, not content; use equals() to compare strings and other objects.
8. Logical Operators
&& (AND), || (OR) and ! (NOT) combine boolean conditions. && and || short-circuit: if the left side already decides the result, the right side is not evaluated, which makes checks like s != null && s.isEmpty() safe.
9. Bitwise and Shift Operators
These work on the individual bits of integers: & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), >> (signed right shift) and >>> (unsigned right shift). They are used for flags, masks, hashing and low-level performance tricks.
10. Ternary Operator
condition ? valueIfTrue : valueIfFalse is a compact if-else that produces a value. It is ideal for simple choices such as String label = stock > 0 ? "In stock" : "Sold out";.
11. Operator Precedence and Associativity
When an expression has several operators, precedence decides which runs first (like multiplication before addition in maths), and associativity decides the order for operators of equal precedence.
public class PrecedenceDemo {
public static void main(String[] args) {
System.out.println("2 + 3 * 4 = " + (2 + 3 * 4)); // * before +
System.out.println("(2 + 3) * 4 = " + ((2 + 3) * 4)); // parentheses first
System.out.println("10 - 4 - 3 = " + (10 - 4 - 3)); // left to right
System.out.println("100 / 10 / 5 = " + (100 / 10 / 5)); // left to right
System.out.println("2 + 3 > 4 && 1 < 0 = " + (2 + 3 > 4 && 1 < 0));
int x, y;
x = y = 7; // assignment: right to left
System.out.println("x = " + x + ", y = " + y);
System.out.println("Sum: " + 2 + 3); // string concatenation first
System.out.println("Sum: " + (2 + 3)); // brackets fix it
}
}
2 + 3 * 4 = 14 (2 + 3) * 4 = 20 10 - 4 - 3 = 3 100 / 10 / 5 = 2 2 + 3 > 4 && 1 < 0 = false x = 7, y = 7 Sum: 23 Sum: 5
The last two lines show a classic trap: "Sum: " + 2 + 3 is evaluated left to right, so 2 is joined to the string first, then 3.
| Precedence (high to low) | Operators | Associativity |
|---|---|---|
| 1. Postfix | x++ x-- | Left to right |
| 2. Unary | ++x --x +x -x ! ~ (type) | Right to left |
| 3. Multiplicative | * / % | Left to right |
| 4. Additive | + - | Left to right |
| 5. Shift | << >> >>> | Left to right |
| 6. Relational | < > <= >= instanceof | Left to right |
| 7. Equality | == != | Left to right |
| 8. Bitwise AND | & | Left to right |
| 9. Bitwise XOR | ^ | Left to right |
| 10. Bitwise OR | | | Left to right |
| 11. Logical AND | && | Left to right |
| 12. Logical OR | || | Left to right |
| 13. Ternary | ? : | Right to left |
| 14. Assignment and lambda | = += -= *= /= %= &= ^= |= <<= >>= >>>= -> | Right to left |
12. Evaluation Order of Operands
Java guarantees that operands are evaluated left to right, and that all operands are evaluated before the operator is applied (except for the short-circuit operators and the ternary operator). This makes expressions with side effects, such as method calls, predictable, but code is still clearer when each statement does one thing.
13. Summary
- Operators act on operands to form expressions; they can be unary, binary or ternary.
- Categories: arithmetic, unary, assignment, relational, logical, bitwise/shift, ternary and
instanceof. - Precedence and associativity decide the order of evaluation; parentheses override them.
- Watch for integer division,
==on objects and string concatenation order.
Related Tutorials
Reference: dev.java: Using Operators