Java Operators
A complete overview of Java operators — arithmetic, assignment, comparison, logical, bitwise, and ternary — with usage examples.
Operators are the small symbols that let you do something with values: add them, compare them, combine boolean tests, set bits, decide between two expressions. This chapter is a roadmap to all of Java's operator categories — each category has its own dedicated chapter that goes deeper.
The categories
Java's operators fall into seven groups:
| Category | Operators | Used for |
|---|---|---|
| Arithmetic | + - * / % | Numeric math |
| Unary | + - ++ -- ! | Sign, increment/decrement, logical NOT |
| Assignment | = += -= *= /= %= &= |= ^= <<= >>= >>>= | Set a variable |
| Comparison | == != > < >= <= | Compare values |
| Logical | && || ! | Combine booleans |
| Bitwise | & | ^ ~ << >> >>> | Manipulate bits |
| Ternary | ? : | Inline conditional |
There's also instanceof (type check) and new (object creation) which behave like operators but are tied to specific language features.
Arithmetic
The five arithmetic operators do what they do in any language, with one quirk:
int sum = 7 + 3; // 10
int diff = 7 - 3; // 4
int product = 7 * 3; // 21
int quotient = 7 / 3; // 2 ← integer division truncates
int remainder = 7 % 3; // 1For real-number division, at least one operand must be double (or cast to one). See Java Arithmetic Operators for the full treatment.
Unary
int x = 5;
int neg = -x; // -5
int inc = ++x; // pre-increment: x becomes 6, inc is 6
int post = x++; // post-increment: post is 6, then x becomes 7
boolean b = !true; // false++ and -- exist as prefix and postfix forms. The prefix changes the variable then yields the new value; postfix yields the old value then changes the variable. Use them only on their own line if you can — embedded in expressions they're a frequent source of bugs.
Assignment
The basic assignment is =. The compound forms combine an operation with assignment:
int total = 0;
total += 5; // total = total + 5
total *= 2; // total = total * 2
total -= 1; // total = total - 1The compound forms also work with bitwise operators (&= |= ^=) and with shifts (<<= >>= >>>=). A compound assignment also performs an implicit cast back to the variable's type, so byte b = 10; b += 5; compiles even though b + 5 is an int — a plain b = b + 5; would not. See Java Assignment Operators for the details.
Comparison
boolean a = (5 > 3); // true
boolean b = (5 == 5); // true
boolean c = (5 != 5); // false== works on primitives and references. On references it tests object identity (same object in memory), not equality of contents. To compare object contents use .equals():
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2); // false — different objects
System.out.println(s1.equals(s2)); // true — same contentsThis trips up everyone the first time. The Java Comparison Operators chapter shows why.
Logical
The three logical operators combine booleans:
boolean a = true && false; // false
boolean b = true || false; // true
boolean c = !true; // false&& and || short-circuit — they stop evaluating once the result is known. This is essential when one side might throw:
if (list != null && list.size() > 0) { /* ... */ } // safe — short-circuitBecause && stops at the first false, list.size() is never called when list is null, so there's no NullPointerException. The Java Logical Operators chapter covers short-circuiting and the non-short-circuit &/| forms in full.
Bitwise
For manipulating individual bits:
int and = 0b1100 & 0b1010; // 0b1000 = 8
int or = 0b1100 | 0b1010; // 0b1110 = 14
int xor = 0b1100 ^ 0b1010; // 0b0110 = 6
int not = ~0; // -1 — all bits flipped
int left = 1 << 4; // 16 — shift left
int right = 16 >> 2; // 4 — shift right (sign-preserving)
int uright = -1 >>> 28; // 15 — unsigned shift rightMost everyday Java code uses these only for flags, hashing, and low-level I/O. Java Bitwise Operators goes deeper.
Ternary
The ternary operator chooses between two expressions:
int x = 10;
String parity = (x % 2 == 0) ? "even" : "odd"; // "even"It evaluates to a value, so it's most useful as the right-hand side of an assignment or inside a larger expression. For multi-step decisions, use a real if/else for readability.
Operator precedence
When several operators appear in one expression, Java evaluates them in a fixed order. The most important precedences, highest first:
++,--, unary-,!,~*,/,%+,-<<,>>,>>><,<=,>,>=,instanceof==,!=&(bitwise AND)^(bitwise XOR)|(bitwise OR)&&||? :=,+=,-=, etc.
In doubt, add parentheses. They cost nothing at runtime and make intent obvious to the next reader.
A demonstration
What's next
The category-by-category chapters that follow drill into each operator group. Java Arithmetic Operators is up next.