Operators in Java
Learn about arithmetic, comparison, logical, bitwise, and assignment operators in Java with practical Docker-ready examples
Operators are the workhorses of every Java expression. They combine values, compare them, and update state — and because Java is a statically and strongly typed language, the type of each operand determines exactly how an operator behaves. The same + symbol adds two integers, adds two doubles, or concatenates strings, depending on what sits on either side of it.
Java inherits much of its operator syntax from C and C++, but with stricter rules: there is no implicit conversion between booleans and integers, integer overflow wraps silently, and division behaves very differently for integer and floating-point types. Understanding these details early will save hours of debugging later.
In this tutorial you’ll work with arithmetic, comparison, logical, bitwise, and assignment operators, see how operator precedence is resolved, and learn the quirks every Java developer eventually encounters.
Arithmetic Operators
Java provides the standard set of arithmetic operators: +, -, *, /, and % (modulo). The catch with / is that when both operands are integers, the result is also an integer — the fractional part is discarded.
Create a file named Arithmetic.java:
| |
Notice how counter++ (postfix) returns the old value before incrementing, while ++counter (prefix) increments first and then returns the new value.
Comparison and Logical Operators
Comparison operators always produce a boolean (true or false). Logical operators combine booleans. Java’s && and || are short-circuiting — they stop evaluating as soon as the result is known.
Create a file named Logic.java:
| |
Note that comparing object references with == checks identity, not equality of contents — for strings and other objects, use .equals(). We stick with primitive comparisons here to keep things focused.
Bitwise and Assignment Operators
Java offers the full set of C-style bitwise operators: &, |, ^, ~, <<, >>, and >>> (unsigned right shift, unique to Java). Compound assignment operators like +=, -=, *=, /=, and %= combine an operation with assignment.
Create a file named BitsAndAssign.java:
| |
Operator Precedence and String Concatenation
Java evaluates operators in a strict precedence order: unary operators bind tightest, then multiplicative (*, /, %), then additive (+, -), then shifts, comparisons, equality, bitwise, logical, and finally assignment. When in doubt, parentheses make intent clear and cost nothing.
The + operator also performs string concatenation when at least one operand is a String. This interacts with left-to-right evaluation in ways that surprise newcomers.
Create a file named Precedence.java:
| |
Running with Docker
| |
Expected Output
Running Arithmetic:
a + b = 22
a - b = 12
a * b = 85
a / b = 3
a % b = 2
x / y = 3.4
counter++ = 10
after: counter = 11
++counter = 12
Running Logic:
age == 25: true
age != 30: true
age > 18: true
age <= 21: false
canDrive: true
isTeen: true
!canDrive: false
safe: false
Running BitsAndAssign:
flags & mask = 2
flags | mask = 14
flags ^ mask = 12
~flags = -11
flags << 2 = 40
flags >> 1 = 5
-8 >>> 1 = 2147483644
total = 4
Running Precedence:
2 + 3 * 4 = 14
(2 + 3) * 4 = 20
Sum: 12
Sum: 3
Grade: Pass
Key Concepts
- Integer vs floating-point division —
17 / 5yields3, while17.0 / 5.0yields3.4. Mixing types promotes the result to the wider type. - Short-circuit evaluation —
&&and||skip the right operand once the answer is determined, which is useful for guarding against null or division by zero. ==on objects checks identity — Use.equals()to compare object contents likeStringvalues. This tutorial used only primitives where==is safe.- Unsigned right shift
>>>— Java is one of the few languages with a dedicated unsigned shift;>>preserves the sign bit,>>>does not. - String concatenation is left-to-right —
"x: " + 1 + 2becomes"x: 12", not"x: 3". Wrap arithmetic in parentheses when concatenating. - Compound assignment includes an implicit cast —
byte b = 10; b += 200;compiles, whileb = b + 200;does not. The compound form silently narrows the result. - Use parentheses for clarity — Java’s precedence rules are well-defined, but explicit grouping makes code easier to read and review.
Running Today
All examples can be run using Docker:
docker pull eclipse-temurin:21-jdk
Comments
Loading comments...
Leave a Comment