DEV Community

Cover image for Arithmetic Operators in Java
Rajesh Bhola
Rajesh Bhola

Posted on

Arithmetic Operators in Java

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 + 515
- Subtraction 10 - 55
* Multiplication 10 * 550
/ Division 10 / 52
% Modulus (Remainder) 10 % 31

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
Enter fullscreen mode Exit fullscreen mode

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, and char are automatically promoted to int before 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');
Enter fullscreen mode Exit fullscreen mode

Output

195
Enter fullscreen mode Exit fullscreen mode

Explanation

'a' = 97
'b' = 98

97 + 98 = 195
Enter fullscreen mode Exit fullscreen mode

System.out.println('a' + 1);
Enter fullscreen mode Exit fullscreen mode

Output

98
Enter fullscreen mode Exit fullscreen mode

System.out.println('a' + 1.2);
Enter fullscreen mode Exit fullscreen mode

Output

98.2
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

Compile-time error

possible lossy conversion from int to byte
Enter fullscreen mode Exit fullscreen mode

Why?

Even though both operands are byte, Java promotes them to int.

byte + byte
↓

int
Enter fullscreen mode Exit fullscreen mode

So,

a + b
Enter fullscreen mode Exit fullscreen mode

produces an int, not a byte.

To assign the result back to a byte, an explicit cast is required.

byte c = (byte)(a + b);
Enter fullscreen mode Exit fullscreen mode

Rule 2: Integral Arithmetic Has No Infinity

Integral data types include:

  • byte
  • short
  • int
  • long

These types cannot represent Infinity.

Therefore, dividing by zero throws an exception.

System.out.println(10 / 0);
Enter fullscreen mode Exit fullscreen mode

Output

Exception in thread "main"
java.lang.ArithmeticException: / by zero
Enter fullscreen mode Exit fullscreen mode

Rule 3: Floating-Point Arithmetic Supports Infinity

Floating-point types are:

  • float
  • double

Unlike integral types, they support special IEEE 754 values.

  • Positive Infinity
  • Negative Infinity

Example

System.out.println(10 / 0.0);
Enter fullscreen mode Exit fullscreen mode

Output

Infinity
Enter fullscreen mode Exit fullscreen mode

System.out.println(-10 / 0.0);
Enter fullscreen mode Exit fullscreen mode

Output

-Infinity
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Output

NaN
Enter fullscreen mode Exit fullscreen mode

Another example

System.out.println(-0.0 / 0.0);
Enter fullscreen mode Exit fullscreen mode

Output

NaN
Enter fullscreen mode Exit fullscreen mode

Compare with Integral Arithmetic

System.out.println(0 / 0);
Enter fullscreen mode Exit fullscreen mode

Output

ArithmeticException
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Output

false
Enter fullscreen mode Exit fullscreen mode

Similarly,

System.out.println(10 == Float.NaN);
Enter fullscreen mode Exit fullscreen mode

Output

false
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Only != returns true.

System.out.println(10 != Float.NaN);         // true
System.out.println(Float.NaN != Float.NaN);  // true
Enter fullscreen mode Exit fullscreen mode

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, and char are promoted to int during 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 NaN using ==. Instead, use Float.isNaN() or Double.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, and char are automatically promoted to int.
  • Integral division by zero throws an ArithmeticException.
  • Floating-point division by zero produces Infinity or -Infinity.
  • 0.0 / 0.0 produces NaN instead of an exception.
  • NaN is 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)