Arithmetic Operators
Arithmetic operators in Java are used to perform basic mathematical calculations such as addition, subtraction, multiplication, division, and remainder. They operate on numeric data types and are fundamental to business logic, calculations, and algorithms.
This topic is very common in interviews and critical for avoiding logical and runtime errors.
What Are Arithmetic Operators?
- Used to perform mathematical operations
- Work with primitive numeric types (byte, short, int, long, float, double, char)
- Result type depends on operand types and promotion rules
List of Arithmetic Operators in Java
| Operator | Name | Description |
|---|---|---|
| + | Addition | Adds two operands |
| - | Subtraction | Subtracts right operand from left |
| * | Multiplication | Multiplies operands |
| / | Division | Divides left operand by right |
| % | Modulus | Returns remainder |
1. Addition Operator (+)
Numeric Addition
int a = 10;
int b = 20;
int sum = a + b; // 30
String Concatenation (Important Special Case)
String s = "Java";
int v = 8;
System.out.println(s + v); // Java8
Why it matters: + is overloaded for String concatenation.
Evaluation Order Example
System.out.println(10 + 20 + "Java"); // 30Java
System.out.println("Java" + 10 + 20); // Java1020
2. Subtraction Operator (-)
int a = 50;
int b = 20;
int result = a - b; // 30
Unary Minus
int x = 10;
int y = -x; // -10
3. Multiplication Operator (*)
int a = 5;
int b = 4;
int product = a * b; // 20
With Floating-Point Numbers
double d = 2.5 * 4; // 10.0
4. Division Operator (/)
Integer Division (Very Important)
int a = 10;
int b = 3;
int result = a / b; // 3
Why it matters: Decimal part is discarded, not rounded.
Floating-Point Division
double x = 10.0 / 3; // 3.3333...
Division by Zero
int x = 10 / 0; // ❌ ArithmeticException
double y = 10.0 / 0; // Infinity
5. Modulus Operator (%)
Returns the remainder after division.
int a = 10;
int b = 3;
int r = a % b; // 1
Common Use Cases
- Check even/odd
- Cyclic logic
- Remainder-based conditions
if (num % 2 == 0) {
System.out.println("Even");
}
Type Promotion in Arithmetic Operations (Interview Focus)
Rule 1: Smaller Types Promoted to int
byte a = 10;
byte b = 20;
int c = a + b; // result is int
Rule 2: Mixed Types → Higher Type Wins
int a = 10;
double b = 2.5;
double c = a + b; // double
Rule 3: Explicit Cast Needed for Smaller Type Assignment
byte b = (byte) (10 + 20);
Arithmetic Operators with char
char c1 = 'A'; // 65
char c2 = 'B'; // 66
int sum = c1 + c2; // 131
Overflow & Underflow (Critical Concept)
Overflow Example
byte b = 127;
b++; // becomes -128
Underflow Example
byte b = -128;
b--; // becomes 127
Common Beginner Mistakes
- Assuming integer division gives decimal result
- Forgetting operator precedence
- Ignoring overflow scenarios
- Confusing % with percentage
- Unexpected results with String concatenation
Interview-Ready Answers
Short Answer
Arithmetic operators in Java are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and modulus.
Detailed Answer
Java provides arithmetic operators (+, -, *, /, %) to perform mathematical calculations on numeric data types. These operators follow type promotion rules, integer division behavior, and support special cases like String concatenation with +.
Key Takeaway
Arithmetic operators are simple but powerful. Understanding type promotion, integer division, and operator behavior is essential for writing correct and predictable Java logic.