Operators & expressions
Arithmetic, assignment, increment, comparison, logical and ternary operators, plus precedence.
Operators are the symbols that combine values into new values: + adds, > compares, && combines conditions. An expression is any piece of code that produces a value, like price * quantity or age >= 18. This lesson walks through every operator you will use day to day.
Arithmetic operators#
The remainder operator is handy: n % 2 == 0 tests for even numbers, and n % 10 gives the last digit.
Warning: integer division by zero (
5 / 0) throws anArithmeticExceptionat runtime. Floating-point division by zero givesInfinity(orNaNfor0.0 / 0) instead.
Assignment and compound assignment#
= stores a value. Compound operators update a variable in place:
Compound operators include a hidden cast. byte b = 10; b += 5; compiles, but b = b + 5; does not, because b + 5 is an int.
Increment and decrement#
++ adds one and -- subtracts one. The position matters when the expression's value is used:
Tip: use ++/-- as standalone statements (i++;) and avoid burying them inside bigger expressions.
Comparison (relational) operators#
These always produce a boolean:
For objects such as
String,==asks "is this the same object?" not "do they contain the same text?". Usea.equals(b)to compare contents. Fordouble, avoid==on computed values; compare with a tolerance:Math.abs(x - y) < 1e-9.
Logical operators#
Short-circuiting means && stops at the first false and || stops at the first true. That is why name != null && name.length() > 3 never throws a NullPointerException. The single-character versions & and | always evaluate both sides; you will rarely want them on booleans.
The ternary operator#
condition ? valueIfTrue : valueIfFalse is a compact if/else that produces a value:
Keep ternaries short. Nesting them quickly becomes unreadable.
Bitwise and shift operators#
These work on the individual bits of integers. You will see them in low-level code, flags and hashing:
Operator precedence#
When an expression has several operators, precedence decides which runs first. From highest to lowest (simplified):
The last three lines are a classic trap. + is evaluated left to right, and as soon as one side is a String, it means "join". When in doubt, add parentheses. They cost nothing and make intent obvious.
Handy Math methods#
Java has no ^ power operator (^ is XOR!). Use the Math class instead:
Math.floorMod always returns a non-negative result for a positive divisor, which is often what you want for things like "wrap around a circular list".
Common mistakes#
- Writing
2 ^ 3expecting 8. It is XOR and gives 1. UseMath.pow(2, 3). - Using
=(assignment) where you meant==(comparison). Withints the compiler catches it; withbooleans,if (done = true)compiles and is always true! - Forgetting that
"Total: " + a + bjoins text instead of adding numbers. - Dividing integers when you wanted a decimal result.
What's next#
You can now build expressions. Next we make programs interactive by reading input from the keyboard with Scanner.
Check your understanding
Quick quiz
1.What does this print?
int x = 5; int y = x++ + ++x; System.out.println(y);2.Why is
if (obj != null && obj.isReady())safe even whenobjis null?3.What is the value of
10 - 2 * 3 + 8 / 4?
Finished reading?
Mark this lesson complete to track your progress.