
7 Arithmetic Operators in Java: A Reliable Reference
Arithmetic Operators in Java
Arithmetic operators in Java are fundamental and allow us to perform basic mathematical operations such as addition, subtraction, multiplication, division, and more. These operators work with both integer and floating-point numbers, making them essential for calculations in Java applications.
Types of Arithmetic Operators in Java
Java provides the following arithmetic operators:
Operator | Description | Example |
---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (Remainder) | a % b |
++ | Increment | a++ or ++a |
-- | Decrement | a-- or --a |
Let’s explore these operators with working examples.
Addition (+
)
The addition operator +
adds two numbers together.
Example:
public class AdditionExample { public static void main(String[] args) { int a = 10; int b = 20; int sum = a + b; System.out.println("Sum: " + sum); // Output: Sum: 30 } }
Subtraction (-
)
The subtraction operator -
subtracts the second operand from the first.
Example:
public class SubtractionExample { public static void main(String[] args) { int a = 50; int b = 30; int difference = a - b; System.out.println("Difference: " + difference); // Output: Difference: 20 } }
Multiplication (*
)
The multiplication operator *
multiplies two numbers.
Example:
public class MultiplicationExample { public static void main(String[] args) { int a = 5; int b = 4; int product = a * b; System.out.println("Product: " + product); // Output: Product: 20 } }
Division (/
)
The division operator /
divides the first operand by the second.
Example:
public class DivisionExample { public static void main(String[] args) { int a = 20; int b = 4; int quotient = a / b; System.out.println("Quotient: " + quotient); // Output: Quotient: 5 } }
Important Note:
- If both operands are integers, Java performs integer division, truncating any decimal values.
- If at least one operand is a floating-point number, the result is a floating-point division.
Example of floating-point division:
public class FloatDivisionExample { public static void main(String[] args) { double a = 20; double b = 3; double result = a / b; System.out.println("Result: " + result); // Output: Result: 6.666666666666667 } }
Modulus (%
)
The modulus operator %
returns the remainder of division.
Example:
public class ModulusExample { public static void main(String[] args) { int a = 10; int b = 3; int remainder = a % b; System.out.println("Remainder: " + remainder); // Output: Remainder: 1 } }
Increment (++
)
The increment operator ++
increases a variable’s value by 1.
Types of Increment Operators
- Post-increment (a++): Returns the current value, then increases by 1.
- Pre-increment (++a): Increases by 1, then returns the new value.
Example:
public class IncrementExample { public static void main(String[] args) { int a = 5; System.out.println("Post-increment: " + (a++)); // Output: 5 System.out.println("After post-increment: " + a); // Output: 6 int b = 5; System.out.println("Pre-increment: " + (++b)); // Output: 6 } }
Decrement (--
)
The decrement operator --
decreases a variable’s value by 1.
Types of Decrement Operators
- Post-decrement (a–): Returns the current value, then decreases by 1.
- Pre-decrement (–a): Decreases by 1, then returns the new value.
Example:
public class DecrementExample { public static void main(String[] args) { int a = 5; System.out.println("Post-decrement: " + (a--)); // Output: 5 System.out.println("After post-decrement: " + a); // Output: 4 int b = 5; System.out.println("Pre-decrement: " + (--b)); // Output: 4 } }
Operator Precedence and Associativity
Arithmetic operations in Java follow a specific order, known as operator precedence. Multiplication and division have higher precedence than addition and subtraction.
Example:
public class OperatorPrecedence { public static void main(String[] args) { int result = 10 + 5 * 2; System.out.println("Result: " + result); // Output: 20 } }
Combining Arithmetic with Assignment (+=, -=, *=, /=, %=
)
Java allows shorthand assignment operators for arithmetic operations:
Example:
public class AssignmentOperators { public static void main(String[] args) { int a = 10; a += 5; // Equivalent to a = a + 5; System.out.println("New value of a: " + a); // Output: 15 } }
Best Practices
- Be cautious of integer division when dealing with whole numbers.
- Use
double
orfloat
for precise decimal calculations, especially in financial applications. - Avoid division by zero as it throws an
ArithmeticException
. - Take advantage of compound assignment operators (
+=
,-=
,*=
, etc.) for more concise and readable code.
Real-World Applications
- Banking & Finance: Used in interest calculations, EMI computations, and stock market analysis.
- Gaming: Helps in score calculations, physics simulations, and character movements.
- Data Analytics: Performs statistical calculations and data transformations.
Conclusion
Arithmetic operators in Java are essential for performing calculations. They include addition, subtraction, multiplication, division, modulus, and increment/decrement operators. Understanding these operators is crucial for writing efficient and effective Java programs. Keep coding and experimenting to enhance your understanding!
If you like this article, please comment and share. For more articles, visit my blog. To read about Operators in Java.
About the author : Mohit Jain
I have more than five years of work experience as a Java Developer. I am passionate about teaching and learning new technologies. I specialize in Java 8, Hibernate, Spring Framework, Spring Boot, and databases like MySQL, and Oracle. I like to share my knowledge with others in the form of articles.