Industry Ready Java Spring Boot, React & Gen AI — Live Course
JavaOperators

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:

  • int
  • float
  • double
  • long
  • short
  • byte

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:

OperatorMeaningExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulus (Remainder)a % b

arithmetic

3. Addition (+)

Used for both numeric addition and string concatenation.

Numeric Addition

int a = 10;
int b = 20;
int sum = a + b; // 30

String Concatenation

String result = "Hello " + "World"; // Hello World

Mixed 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 = 10

4. Subtraction (-)

int a = 20;
int b = 5;
int diff = a - b; // 15

Also works with floating-point numbers:

double x = 5.5 - 2.1; // 3.4

5. Multiplication (*)

int a = 6;
int b = 4;
int product = a * b; // 24

6. 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.5

Division by Zero

  • Integer division by zero → ArithmeticException
  • Floating division by zero → Infinity or NaN

Example:

System.out.println(5.0 / 0);   // Infinity
System.out.println(0.0 / 0);   // NaN

7. Modulus (%)

Returns the remainder after division.

int a = 10;
int b = 3;
int remainder = a % b; // 1

Useful for:

  • Checking even/odd numbers
  • Circular patterns
  • Clock calculations
  • Loop cycling

Example:

if (n % 2 == 0) {
    System.out.println("Even");
}

arithmetic

8. Operator Behavior with Different Data Types

Mixing int and double

int a = 5;
double b = 2.0;
double result = a + b; // 7.0

Java 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 required

9. Precedence with Arithmetic Operators

Operator precedence:

  1. *, /, % → higher precedence
  2. +, - → lower precedence

Example:

int result = 10 + 5 * 2; // 20

Correct way if you want addition first:

int result = (10 + 5) * 2; // 30

10. 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; // 5

11. Common Mistakes

  • Assuming 7 / 2 gives 3.5 (integer division returns 3)
  • Forgetting to use parentheses for clarity
  • Misusing % with floating numbers
  • Forgetting type casting for byte and short

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