Menu

Java Operators: Arithmetic, Comparison, Logical, and Assignment

How Java operators work - arithmetic, comparison, logical, assignment, increment, and the ternary operator - plus integer division, precedence, and the gotchas that bite beginners.

This page includes runnable editors. Edit, run, and see output instantly.

What Operators Do

Operators are the symbols that combine values to compute new ones. You already use them informally - + adds, > compares - and Java groups them into a few families: arithmetic, comparison, logical, assignment, and a couple of specials like increment and the ternary.

Most operators take two values (one before, one after the symbol) and produce a result. The trick is knowing what type that result is and the order they apply in - which is where beginners trip up.

Arithmetic Operators

The five arithmetic operators work on numbers: +, -, *, /, and % (remainder).

The % operator is the remainder after division: 17 % 5 is 2 because 5 goes into 17 three times with 2 left over. It is the everyday tool for testing divisibility - n % 2 == 0 is the standard "is n even?" check.

Note also that + does double duty: with numbers it adds, but with a String on either side it joins text.

The first line reads left to right: "Total: " + 3 is already a string, so the 4 is appended as text. Wrap the math in parentheses when you want it computed first.

The Integer-Division Gotcha

This is the single most common surprise. When both operands of / are integers, Java does integer division and discards the fraction - it does not round, it truncates toward zero.

To get a decimal result, make at least one operand a double - either write it with a .0, or cast one side with (double). Casting (double)(5 / 2) does not help, because the integer division 5 / 2 already happened before the cast; you must cast an operand, not the result.

Comparison Operators

Comparison operators ask a yes/no question and return a boolean. There are six: ==, !=, <, >, <=, >=.

Watch the difference between = (assignment - "put this value here") and == (comparison - "are these equal?"). Mixing them up is a classic bug.

One important caveat: == compares object references, not contents. For strings, use .equals().

Use == for primitives like int and boolean, and .equals() whenever you compare the contents of objects such as strings.

Logical Operators

Logical operators combine boolean values: && (and), || (or), and ! (not). They are how you express conditions like "old enough and has a ticket".

&& and || short-circuit: Java stops evaluating as soon as the answer is certain. With &&, if the left side is false the right side is never checked; with ||, if the left side is true the right side is skipped. This is not just an optimization - you can lean on it for safety:

if (text != null && text.length() > 0) { ... }

If text is null, the left side is false and text.length() is never called, avoiding a NullPointerException. Reverse the order and it would crash.

Assignment and Increment

= assigns, but Java also has compound assignments that combine an operation with assignment: +=, -=, *=, /=, %=. And ++ / -- add or subtract one.

++ comes in two flavors: postfix (count++) returns the old value then increments; prefix (++count) increments first then returns the new value. The difference only matters when you use the result in the same expression:

When ++ is on its own line, postfix vs prefix make no difference - so prefer whichever reads clearly and don't cram increments into bigger expressions.

The Ternary Operator

The ternary condition ? a : b is a compact if/else that produces a value: if the condition is true it evaluates to a, otherwise b.

It shines for short either/or assignments. Resist nesting ternaries inside ternaries - that gets unreadable fast, and a plain if/else is clearer.

Precedence and Parentheses

Operators apply in a fixed order, much like math: *, /, % bind tighter than + and -, which bind tighter than comparisons, which bind tighter than &&, then ||.

You don't need to memorize the full table. When an expression mixes families or you have to pause to work out the order, add parentheses - they cost nothing and make intent obvious to the next reader.

Next: Type Casting

Several gotchas here - integer division, mixing int and double - come down to types. Converting between number types deliberately is called casting, and that is the next page.

Frequently Asked Questions

What does the % operator do in Java?

% is the remainder (modulo) operator - it gives what is left over after integer division. 7 % 3 is 1, and 10 % 2 is 0. It is the standard way to test divisibility (n % 2 == 0 means n is even) and to wrap a value into a range.

Why does 5 / 2 give 2 in Java instead of 2.5?

When both operands are integers, / performs integer division and throws away the fractional part - so 5 / 2 is 2, not 2.5. To get 2.5 you need at least one operand to be a double: write 5.0 / 2, 5 / 2.0, or cast one side with (double) 5 / 2.

What is the difference between == and equals() in Java?

== compares primitives by value, but for objects (including String) it compares references - whether they are the same object in memory. To compare object contents, use .equals(). Comparing two strings with == may return false even when the text matches, so always use "a".equals(b) for strings.

Coddy programming languages illustration

Learn to code with Coddy

Get started