Arithmetic Operators in Java
Arithmetic operators in Java are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and finding the remainder. These operators work with numeric data types like int, float, double, and long.
In this tutorial, you will learn about different types of arithmetic operators in Java with syntax, examples, and practical explanations.
What are Arithmetic Operators in Java?
Arithmetic operators are special symbols used to perform mathematical calculations between two or more values. Java provides several arithmetic operators that help developers perform numerical operations easily.
For example, the + operator is used to add two numbers, while the - operator is used to subtract one number from another.
Types of Arithmetic Operators in Java
Java provides the following arithmetic operators:
- Addition Operator (+)
- Subtraction Operator (-)
- Multiplication Operator (*)
- Division Operator (/)
- Modulus Operator (%)
- Increment Operator (++)
- Decrement Operator (--)
Java Arithmetic Operators Table
| Operator | Name | Description |
|---|---|---|
| + | Addition | Adds two values |
| - | Subtraction | Subtracts one value from another |
| * | Multiplication | Multiplies two values |
| / | Division | Divides one value by another |
| % | Modulus | Returns the remainder after division |
Basic Example of Arithmetic Operators in Java
public class Main {
public static void main(String[] args) {
int a =20;
int b =10;
int sum = a + b;
int difference = a - b;
int multiply = a * b;
int divide = a / b;
int remainder = a % b;
System.out.println(sum);
System.out.println(difference);
System.out.println(multiply);
System.out.println(divide);
System.out.println(remainder);
}
}
10
200
2
0
Addition Operator (+) in Java
The addition operator (+) is used to add two or more numeric values. It combines the values and returns their total result.
int number1 = 15;
int number2 = 25;
int result = number1 + number2;
System.out.println(result);
Subtraction Operator (-) in Java
The subtraction operator (-) is used to subtract one value from another value.
int total = 50;
int used = 20;
int remaining = total - used;
System.out.println(remaining);
Multiplication Operator (*) in Java
The multiplication operator (*) is used to multiply two or more numeric values.
int price = 100;
int quantity = 5;
int total = price * quantity;
System.out.println(total);
Division Operator (/) in Java
The division operator (/) is used to divide one number by another number and returns the quotient.
int number1 = 100;
int number2 = 10;
int result = number1 / number2;
System.out.println(result);
Modulus Operator (%) in Java
The modulus operator (%) returns the remainder after dividing one number by another number.
int number1 = 17;
int number2 = 5;
int result = number1 % number2;
System.out.println(result);
Increment and Decrement Operators in Java
Java also provides increment (++) and decrement (--) operators to increase or decrease the value of a variable by 1.
int value = 10 value++;
System.out.println(value);
value--;
System.out.println(value);
10
Rules for Using Arithmetic Operators in Java
While using arithmetic operators in Java, some important rules should be followed to avoid unexpected results.
Numeric Values
Arithmetic operators can be used with numeric data types such as int, float, double, and long.
Division by Zero
Division by zero is not allowed and may cause an ArithmeticException when using integer values.
Data Type
The result of an arithmetic operation depends on the data type of operands.
Operator Priority
Java follows operator precedence rules while evaluating arithmetic expressions.
Arithmetic Operators Precedence in Java
When multiple arithmetic operators are used in a single expression, Java follows a specific order called operator precedence.
- Parentheses ( ) have the highest priority.
- Multiplication (*), Division (/), and Modulus (%) are evaluated next.
- Addition (+) and Subtraction (-) are evaluated at the end.
Example of Arithmetic Operator Precedence
int result = 10 + 5 * 2;
System.out.println(result);
Advantages of Arithmetic Operators in Java
Easy Calculations
Arithmetic operators make mathematical calculations simple and readable.
Fast Processing
These operators perform calculations quickly and efficiently.
Used Everywhere
Arithmetic operations are commonly used in applications like banking, games, and data processing systems.
Beginner Friendly
Arithmetic operators are simple and help beginners understand Java logic.
Common Mistakes While Using Arithmetic Operators
- Performing division by zero.
- Using incorrect data types for calculations.
- Forgetting operator precedence rules.
- Mixing integer and decimal calculations without understanding the result.
Summary
Arithmetic operators in Java are used to perform mathematical operations on numeric values. Java provides operators like addition, subtraction, multiplication, division, modulus, increment, and decrement.
Understanding arithmetic operators is important because they are widely used in Java programs for calculations and logical operations.