Working with Binary Arithmetic Operators
Binary arithmetic operators take two operands and produce a numeric result. Java supports addition, subtraction, multiplication, division, and modulus — each with specific rules for integer vs. floating-point behavior.
The Five Arithmetic Operators
| Operator | Name | Example | Result |
|---|---|---|---|
+ |
Addition | 5 + 3 |
8 |
- |
Subtraction | 5 - 3 |
2 |
* |
Multiplication | 5 * 3 |
15 |
/ |
Division | 5 / 3 |
1 (integer) |
% |
Modulus (remainder) | 5 % 3 |
2 |
Integer Division
When both operands are integral types (byte, short, int, long, char), / performs integer division — it discards the fractional part (truncates toward zero).
javaSystem.out.println(10 / 3); // 3 (not 3.33...)
System.out.println(-10 / 3); // -3 (truncates toward zero, not -4)
System.out.println(10 / -3); // -3Dividing an integer by zero throws
ArithmeticException: / by zeroat runtime.
javaint x = 5 / 0; // throws ArithmeticException at runtimeFloating-Point Division
If either operand is float or double, the result is floating-point and division never truncates.
javaSystem.out.println(10.0 / 3); // 3.3333333333333335
System.out.println(10 / 3.0); // 3.3333333333333335
System.out.println(10.0 / 0); // Infinity (no exception)
System.out.println(0.0 / 0.0); // NaNFloating-point division by zero yields Infinity or NaN — it does not throw an exception.
Modulus Operator %
Returns the remainder after division. Works for both integers and floating-point.
javaSystem.out.println(10 % 3); // 1 (10 = 3*3 + 1)
System.out.println(-10 % 3); // -1 (sign follows the dividend)
System.out.println(10 % -3); // 1
System.out.println(10.5 % 3); // 1.5Integer modulus by zero also throws ArithmeticException.
String Concatenation with +
When either operand of + is a String, the operator performs concatenation, not addition. Evaluation is left-to-right.
javaSystem.out.println("value: " + 1 + 2); // "value: 12" (left-to-right)
System.out.println("value: " + (1 + 2)); // "value: 3" (parentheses first)
System.out.println(1 + 2 + " value"); // "3 value" (1+2 = 3 first, then concat)Numeric Promotion Rules
Before a binary arithmetic operation, Java automatically promotes operands according to these rules (applied in order):
- If either operand is
double, the other is promoted todouble. - Else if either operand is
float, the other is promoted tofloat. - Else if either operand is
long, the other is promoted tolong. - Otherwise, both operands are promoted to
int— even if both arebyte,short, orchar.
javashort s1 = 10;
short s2 = 20;
// short s3 = s1 + s2; // compile error: result is int
int s3 = s1 + s2; // correctjavaint i = 5;
long l = 10L;
long result = i + l; // i promoted to longjavalong l = 10L;
float f = 2.5f;
float r = l + f; // l promoted to floatWidening Promotion in Assignments
A common exam pattern combines promotion with assignment:
javabyte b = 10;
short s = 20;
// b = b + s; // compile error: result is int, cannot assign to byte
b = (byte)(b + s); // explicit cast requiredInteger Overflow
When an integer result exceeds the range of its type, it wraps around silently — no exception is thrown.
javaint max = Integer.MAX_VALUE; // 2_147_483_647
System.out.println(max + 1); // -2_147_483_648 (wraps around)Use long when values may exceed int range:
javalong safe = (long) Integer.MAX_VALUE + 1; // 2_147_483_648LPractical Examples
java// Checking even/odd
int n = 7;
boolean isEven = (n % 2 == 0); // false
// Extracting digits
int number = 1234;
int lastDigit = number % 10; // 4
int remaining = number / 10; // 123
// Average with promotion
int a = 7, b = 3;
double avg = (double)(a + b) / 2; // cast before division: 5.0
// double wrong = (double)(a + b / 2); // 3+1 = 4 cast to 4.0 (wrong)Key Rules to Remember
- Integer division truncates toward zero — it does not round.
- Integer division by zero throws
ArithmeticException; floating-point division by zero yieldsInfinityorNaN. byte,short, andcharalways promote tointin arithmetic — assigning the result back requires a cast.- The sign of
%matches the sign of the dividend. - With
+, if either operand is aString, concatenation occurs — not addition.
References
- Oracle Docs — Arithmetic Operators
- OCP Oracle Certified Professional Java SE 21 Study Guide — Chapter 2