Arithmetic Operators
5 min read ·
Arithmetic operators are used to perform mathematical calculations on numeric data types like int, float, double, long, short and byte.
These operators are mostly used in calculations, loops, billing systems, banking applications and scoring systems.
Arithmetic Operators Table
| Operator | Name | Description | Example | Result |
|---|---|---|---|---|
| + | Addition | Adds two values | 10 + 5 | 15 |
| - | Subtraction | Subtracts second value from first | 10 - 5 | 5 |
| * | Multiplication | Multiplies two values | 10 * 5 | 50 |
| / | Division | Divides first value by second | 10 / 5 | 2 |
| % | Modulus | Returns remainder | 10 % 3 | 1 |
1. Addition Operator +
Used to add two numbers.
Example
Output
Sum: 50
Addition with Different Data Types
Java automatically converts int to double.
2. Subtraction Operator -
Used to subtract one number from another.
Example
Output
Remaining Marks: 28
3. Multiplication Operator *
Used to multiply two numbers.
Example
Output
Area: 20
4. Division Operator /
Used to divide one number by another.
Important Concept Integer Division
If both values are integers, Java removes decimal part.
Example 1 Integer Division
Output
Result: 3
Example 2 Decimal Division
Output
Result: 3.3333333333333335
Example 3 Using Casting
Caution
Division by zero causes ArithmeticException when using integers.
Example Division by Zero
This program will crash at runtime.
5. Modulus Operator %
Returns the remainder after division.
Example
Output
Remainder: 1
Real World Usage
Checking Even or Odd
Arithmetic Operators with Mixed Data Types
When different numeric types are used together, Java automatically promotes smaller type to larger type.
Example
byte and int result becomes int
int and double result becomes double
Operator Precedence in Arithmetic
Operator precedence defines which operation is executed first.
Order of execution
1 Multiplication
2 Division
3 Modulus
4 Addition
5 Subtraction
Example
Output
20
Because multiplication runs first.
Using parentheses changes priority.
Output
30
Practice Exercise
1 Create a program to calculate simple interest using formula
SimpleInterest = (P * R * T) / 100
2 Write a program to calculate area of circle
3 Take two numbers and print quotient and remainder separately
Practice these properly because arithmetic operators are base of all programming logic.