Arithmetic Operators
1. Introduction
Arithmetic operators in Java are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and modulus.
These operators work with primitive numeric types like:
intfloatdoublelongshortbyte
Understanding arithmetic operators is foundational for performing calculations in Java programs, including loops, conditions, formulas, financial calculations, and more.
2. Types of Arithmetic Operators
Java provides five primary arithmetic operators:
| Operator | Meaning | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (Remainder) | a % b |

3. Addition (+)
Used for both numeric addition and string concatenation.
Numeric Addition
int a = 10;
int b = 20;
int sum = a + b; // 30String Concatenation
String result = "Hello " + "World"; // Hello WorldMixed addition:
System.out.println("Total = " + 5 + 5);
// Output: Total = 55 (because first part is string)Use parentheses for numeric addition:
System.out.println("Total = " + (5 + 5));
// Output: Total = 104. Subtraction (-)
int a = 20;
int b = 5;
int diff = a - b; // 15Also works with floating-point numbers:
double x = 5.5 - 2.1; // 3.45. Multiplication (*)
int a = 6;
int b = 4;
int product = a * b; // 246. Division (/)
Division behaves differently for integers and floating-point types.
Integer Division
int a = 7;
int b = 2;
int result = a / b; // 3 (decimal part discarded)Floating-Point Division
double x = 7.0 / 2; // 3.5Division by Zero
- Integer division by zero → ArithmeticException
- Floating division by zero →
InfinityorNaN
Example:
System.out.println(5.0 / 0); // Infinity
System.out.println(0.0 / 0); // NaN7. Modulus (%)
Returns the remainder after division.
int a = 10;
int b = 3;
int remainder = a % b; // 1Useful for:
- Checking even/odd numbers
- Circular patterns
- Clock calculations
- Loop cycling
Example:
if (n % 2 == 0) {
System.out.println("Even");
}
8. Operator Behavior with Different Data Types
Mixing int and double
int a = 5;
double b = 2.0;
double result = a + b; // 7.0Java automatically promotes smaller types (byte, short) to int in arithmetic expressions.
Example:
byte x = 10;
byte y = 20;
byte z = (byte)(x + y); // x+y becomes int → cast required9. Precedence with Arithmetic Operators
Operator precedence:
*,/,%→ higher precedence+,-→ lower precedence
Example:
int result = 10 + 5 * 2; // 20Correct way if you want addition first:
int result = (10 + 5) * 2; // 3010. Real-World Examples
Example 1: Calculate Area
double radius = 5;
double area = 3.14 * radius * radius;Example 2: Shopping Bill Calculation
double price = 100;
double discount = 10;
double finalPrice = price - (price * discount / 100);Example 3: Convert Seconds to Minutes and Seconds
int total = 125;
int minutes = total / 60; // 2
int seconds = total % 60; // 511. Common Mistakes
- Assuming
7 / 2gives3.5(integer division returns 3) - Forgetting to use parentheses for clarity
- Misusing
%with floating numbers - Forgetting type casting for
byteandshort
12. Summary
- Arithmetic operators perform mathematical computations.
/behaves differently for integers and floating-point values.%gives remainder, useful for patterns, conditions, and cycles.- Operator precedence affects evaluation order.
- Parentheses help make expressions clearer.
How is this guide?
Last updated on
