Operators
In short: Symbols that perform an operation on one or more values — from simple arithmetic to comparisons and assignments.
In more detail: Java groups operators into several categories: arithmetic (+ - * / %), assignment (= += -=), comparison (== != < >), and logical (&& || !) operators. For nested expressions, operator precedence decides which part is evaluated first.
In Depth
| Category | Examples | Purpose |
|---|---|---|
| Arithmetic | + - * / % | arithmetic operations |
| Assignment | = += -= *= /= | assign/update values |
| Comparison | == != < > <= >= | compare two values, result boolean |
| Logical | && || ! | combine boolean expressions |
| Increment/decrement | ++ -- | increase/decrease value by 1 |
| Bitwise | & | ^ ~ << >> >>> | manipulate individual bits |
| Ternary | ? : | compact if-else alternative as an expression |
The bitwise operators (& | ^ ~ << >>) differ only minimally in appearance from the logical ones (&& ||), but behave fundamentally differently: they operate on the binary representation of integers, bit by bit, instead of on boolean values. 5 & 3, for example, returns 1 (binary 101 & 011 = 001), not a boolean value at all — confusing &/| with &&/|| therefore often still compiles (if both operands are int), but returns a completely different result than intended. In everyday application development, bitwise operators are rarely needed — they mainly show up in low-level programming, flags/bitmasks, or performance-critical code.
int a = 5, b = 3;
System.out.println(a & b); // 1 - bitwise AND (binary: 101 & 011 = 001)
System.out.println(a && b == 3); // caution: && expects boolean on the left - a && ... does NOT compile
boolean x = true, y = false;
System.out.println(x & y); // false - the bitwise variant also works on boolean, but ALWAYS evaluates both sides
System.out.println(x && y); // false - && only evaluates y if x is already true (short-circuit)
// Ternary operator - compact alternative to a simple if...else
int age = 20;
String status = (age >= 18) ? "adult" : "minor";Operator precedence in practice
Without parentheses, Java evaluates expressions according to a fixed ranking — multiplication/division bind more strongly than addition/subtraction, comparison operators more strongly than logical combinations, and assignment binds the weakest of all:
int result = 2 + 3 * 4; // 14, not 20 - * binds more strongly than +
boolean check = 5 > 3 && 2 < 1; // (5 > 3) && (2 < 1) = true && false = falseFor more complex, nested expressions, it’s common to add explicit parentheses, even when they wouldn’t strictly be needed under the precedence rules — this makes the intent immediately clear to other readers, without them having to run through the entire ranking in their head. Details on the exact order are in Operator Precedence.
Compound assignment operators
+= -= *= /= are compact shorthand forms that automatically incorporate the original value:
int counter = 10;
counter += 5; // equals counter = counter + 5; -> 15
counter *= 2; // equals counter = counter * 2; -> 30For int values, these compact forms even contain an implicit cast that the spelled-out version wouldn’t have: byte b = 10; b += 5; compiles fine, while b = b + 5; would need an explicit (byte) cast, because b + 5 is evaluated as an int expression.
See also: Arithmetic Operators, Assignment Operators, Comparison Operators, Logical Operators, Operator Precedence