Arithmetic operators are among the most frequently used operators in Java. They allow you to perform mathematical calculations such as addition, subtraction, multiplication, division, and finding the remainder.
Whether you're calculating a total, iterating through loops, or solving programming problems, you'll use arithmetic operators almost every day.
Let's understand how they work, the rules Java follows, and some important interview concepts.
What Are Arithmetic Operators?
Java provides the following arithmetic operators:
| Operator | Description | Example |
|---|---|---|
+ |
Addition |
10 + 5 → 15
|
- |
Subtraction |
10 - 5 → 5
|
* |
Multiplication |
10 * 5 → 50
|
/ |
Division |
10 / 5 → 2
|
% |
Modulus (Remainder) |
10 % 3 → 1
|
Example
int a = 20;
int b = 6;
System.out.println(a + b); // 26
System.out.println(a - b); // 14
System.out.println(a * b); // 120
System.out.println(a / b); // 3
System.out.println(a % b); // 2
Rule 1: Result Type Formula (Type Promotion)
One of the most important rules in Java is:
The result type of an arithmetic operation is always the maximum of
int, the type of the first operand, and the type of the second operand.
In simple words:
-
byte,short, andcharare automatically promoted tointbefore arithmetic operations. - The result is never smaller than
int.
Type Promotion Table
| Expression | Result Type |
|---|---|
byte + byte |
int |
byte + short |
int |
short + short |
int |
char + char |
int |
char + int |
int |
short + long |
long |
int + double |
double |
float + double |
double |
Examples
System.out.println('a' + 'b');
Output
195
Explanation
'a' = 97
'b' = 98
97 + 98 = 195
System.out.println('a' + 1);
Output
98
System.out.println('a' + 1.2);
Output
98.2
Since one operand is double, the final result is also a double.
Why Does This Cause a Compile-Time Error?
Consider the following code:
byte a = 10;
byte b = 20;
byte c = a + b;
Compile-time error
possible lossy conversion from int to byte
Why?
Even though both operands are byte, Java promotes them to int.
byte + byte
↓
int
So,
a + b
produces an int, not a byte.
To assign the result back to a byte, an explicit cast is required.
byte c = (byte)(a + b);
Rule 2: Integral Arithmetic Has No Infinity
Integral data types include:
byteshortintlong
These types cannot represent Infinity.
Therefore, dividing by zero throws an exception.
System.out.println(10 / 0);
Output
Exception in thread "main"
java.lang.ArithmeticException: / by zero
Rule 3: Floating-Point Arithmetic Supports Infinity
Floating-point types are:
floatdouble
Unlike integral types, they support special IEEE 754 values.
- Positive Infinity
- Negative Infinity
Example
System.out.println(10 / 0.0);
Output
Infinity
System.out.println(-10 / 0.0);
Output
-Infinity
No ArithmeticException is thrown.
Rule 4: Floating-Point Arithmetic Supports NaN
NaN stands for Not a Number.
It represents an undefined mathematical result.
Example
System.out.println(0.0 / 0.0);
Output
NaN
Another example
System.out.println(-0.0 / 0.0);
Output
NaN
Compare with Integral Arithmetic
System.out.println(0 / 0);
Output
ArithmeticException
Integral types cannot represent undefined values.
Rule 5: NaN Comparison Behavior (Interview Question)
One of the most surprising behaviors in Java is that NaN is not equal to itself.
Example
System.out.println(Float.NaN == Float.NaN);
Output
false
Similarly,
System.out.println(10 == Float.NaN);
Output
false
All comparison operators return false.
System.out.println(10 < Float.NaN); // false
System.out.println(10 <= Float.NaN); // false
System.out.println(10 > Float.NaN); // false
System.out.println(10 >= Float.NaN); // false
System.out.println(10 == Float.NaN); // false
System.out.println(Float.NaN == Float.NaN); // false
Only != returns true.
System.out.println(10 != Float.NaN); // true
System.out.println(Float.NaN != Float.NaN); // true
Why?
This behavior follows the IEEE 754 Floating-Point Standard, which Java implements.
Summary: ArithmeticException
| Property | Detail |
|---|---|
| Exception Type | ArithmeticException |
| Category | Runtime Exception |
| Occurs In | Integral arithmetic only |
| Never Occurs In | Floating-point arithmetic |
| Caused By |
/ and % with zero divisor |
Quick Memory Trick
| Scenario | Integral (int, byte, long) |
Floating (float, double) |
|---|---|---|
10 / 0 |
❌ ArithmeticException
|
✅ Infinity
|
-10 / 0 |
❌ ArithmeticException
|
✅ -Infinity
|
0 / 0 |
❌ ArithmeticException
|
✅ NaN
|
| Supports Infinity | ❌ No | ✅ Yes |
| Supports NaN | ❌ No | ✅ Yes |
Best Practices
- Remember that
byte,short, andcharare promoted tointduring arithmetic operations. - Don't assume the result type is the same as the operand type.
- Use explicit casting when assigning arithmetic results back to smaller data types.
- Avoid comparing floating-point values with
NaNusing==. Instead, useFloat.isNaN()orDouble.isNaN(). - Be careful when dividing integers by zero, as it throws an
ArithmeticException.
Key Takeaways
- Java provides five arithmetic operators:
+,-,*,/, and%. - Arithmetic operations on
byte,short, andcharare automatically promoted toint. - Integral division by zero throws an
ArithmeticException. - Floating-point division by zero produces
Infinityor-Infinity. -
0.0 / 0.0producesNaNinstead of an exception. -
NaNis not equal to itself, making it a popular Java interview question. - Understanding Java's type promotion rules helps avoid many common compile-time errors.
Happy Coding!
Top comments (0)