skip to content

Arithmetic Operators

The arithmetic operators, including integer division truncating toward zero, the sign behavior of modulo, and prefix versus postfix increment. i++ versus ++i inside an expression is a perennial output-prediction question.

part ofJavaoverview, primer and where to startread it →
on this pageshow

questions

5

What are the arithmetic operators in Java and what does each one do?

level: juniorimportance: must knowfreq 70%

answer

  1. 5 binary: + - * / % ; 2 unary: + -
  2. overloaded = String concatenation
  3. numeric promotion -> result type = widest operand
  4. 5/2 == 3 (int), 5.0/2 == 2.5
  5. / % bind tighter than + -

basics

~10 s

Java has +, -, *, / and % for add, subtract, multiply, divide, and remainder. There are also unary + and - that keep or flip a number's sign.

solid answer

~40 s

Java's binary arithmetic operators are + (addition), - (subtraction), * (multiplication), / (division) and % (remainder/modulo). The + is also overloaded for String concatenation, so when either side is a String the result is text. There are unary + and - operators: unary minus negates its operand, unary plus is essentially a no-op kept for symmetry. Operands first undergo numeric promotion (byte/short/char widen to int; if either side is long/float/double the other widens to match) and the result type follows the wider operand. So 5/2 is 3 (int math) while 5.0/2 is 2.5. Multiplication, division and remainder bind tighter than addition and subtraction; parentheses override that precedence.

code

java · 11 lines
java
int a = 5, b = 2;
System.out.println(a + b);   // 7
System.out.println(a - b);   // 3
System.out.println(a * b);   // 10
System.out.println(a / b);   // 2  (integer division)
System.out.println(a % b);   // 1  (remainder)
System.out.println(-a);      // -5 (unary minus)

System.out.println(5.0 / 2); // 2.5 (one double -> double math)
System.out.println(1 + 2 + "x"); // "3x"  (+ adds, then concatenates)
System.out.println("x" + 1 + 2); // "x12" (left-to-right concatenation)

go deeper

for a junior

Names all five binary operators plus unary minus, and knows 5/2 is 2 because both are ints.

for a middle

Explains numeric promotion and that the result type is the widest operand; predicts the + overloading and concatenation order.

for a senior

Distinguishes int vs float divide-by-zero, mentions silent integer overflow vs floating-point precision loss, and reasons about precedence/associativity cleanly.

for a principal

Frames the rules in terms of the JLS numeric-promotion model and can advise on safe-arithmetic patterns (Math.*Exact, BigDecimal) at a codebase/standards level.

## What 'arithmetic operators' means An **operator** is a symbol that tells the compiler to compute something from one or more values, called **operands**. **Arithmetic operators** do basic math on numbers. Java has five **binary** arithmetic operators (binary = two operands, one on each side): | Operator | Name | `7 ? 2` | |---|---|---| | `+` | addition | `9` | | `-` | subtraction | `5` | | `*` | multiplication | `14` | | `/` | division | `3` (see below) | | `%` | remainder (modulo) | `1` | And two **unary** arithmetic operators (unary = one operand): - **Unary minus** `-x` negates: `-5` is negative five; `-(x)` flips the sign of whatever `x` holds. - **Unary plus** `+x` does nothing useful to the value; it exists for symmetry. (One subtle effect: it forces numeric promotion, so `+aByte` has type `int`.) ## Two facts every junior trips on 1. **`+` is overloaded.** With numbers it adds; if *either* operand is a `String`, `+` means **String concatenation** and the result is a `String`. `1 + 2 + "x"` is `"3x"` (left-to-right: `1+2=3`, then `3 + "x"`), but `"x" + 1 + 2` is `"x12"`. 2. **The result type depends on the operands, not on the variable you store into.** Before computing, Java applies **numeric promotion**: `byte`, `short`, `char` are widened to `int`; then if either operand is `long`/`float`/`double`, the narrower one is widened to match. The operation is done in that widest type, and that is the type of the result. So `5 / 2` is **integer division** giving `3`, while `5.0 / 2` is `2.5`. Storing an `int` result into a `double` variable (`double d = 5/2;`) still gives `3.0` because the division already happened in `int`. ## Precedence and associativity `*`, `/`, `%` have **higher precedence** than `+`, `-`. So `2 + 3 * 4` is `14`, not `20`. Unary `+`/`-` bind tighter than all of them. All binary arithmetic operators are **left-associative**: `a - b - c` is `(a - b) - c`. Use parentheses to make intent explicit and to override precedence. ## Edge behaviour to remember - Integer `/` by zero throws `ArithmeticException`; floating-point `/` by zero gives `Infinity`/`NaN`, never an exception. - Integer arithmetic **wraps around** silently on overflow (it does not throw); floating-point can lose precision. With these rules you can predict the value *and* the type of any arithmetic expression.

  • Why does `double d = 5 / 2;` print 2.0 instead of 2.5?
    Both operands are int, so the division happens in int arithmetic and yields 2; that 2 is then widened to 2.0 when assigned to the double. To get 2.5, make one operand a double, e.g. 5.0 / 2 or (double) 5 / 2.
  • What is the result and type of `'A' + 1`?
    66 of type int. char 'A' is promoted to its code point 65, then + adds 1 in int arithmetic. To get the String "A1" you'd write "" + 'A' + 1.

Think of the operands as deciding the 'currency' of the result: mix in even one double and the whole expression is paid out in doubles; all-int means integer-only change, with the cents (the fraction) thrown away.

saying these in an interview costs you the question

  • Thinking the result type comes from the variable you assign to, rather than from the operands
  • Believing 5/2 returns 2.5 in Java
  • Assuming + always means addition (forgetting String concatenation)
  • Thinking integer overflow throws an exception

context

open as a page

How does integer division work in Java, and how does division-by-zero behave for integers versus floating-point?

level: middleimportance: must knowfreq 65%

basics

~20 s

When both operands are whole numbers, / drops the fraction and rounds toward zero, so 7/2 is 3 and -7/2 is -3. Dividing an integer by 0 throws an error; dividing a double by 0.0 gives Infinity or NaN.

open as a page

What is the difference between prefix and postfix increment/decrement (++x vs x++) in Java?

level: juniorimportance: should knowfreq 60%

basics

~20 s

Both add 1 to the variable. The difference is the value the expression gives: ++x increments first and returns the new value; x++ returns the old value and then increments. Used alone on a line they behave the same.

open as a page

What does the % operator return in Java, and how does the sign of the result behave for negative operands and for floating-point values?

level: middleimportance: should knowfreq 55%

basics

~20 s

% gives the remainder after division. In Java the result takes the sign of the left operand (the dividend), so -7 % 3 is -1, not 2. It also works on doubles: 5.5 % 2 is 1.5.

open as a page

Why do `int` arithmetic and `double` arithmetic produce surprising results (silent overflow, lost precision), and how should you guard against them?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Whole-number math wraps around silently when it gets too big (e.g. a huge int can suddenly go negative). Decimal math (double) can't represent some values exactly, so 0.1 + 0.2 isn't exactly 0.3. Use bigger types, Math.*Exact, or BigDecimal to stay safe.

open as a page