skip to content

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