Java Arithmetic Operators: Tutorial with Code Examples

Arithmetic operators are essential for performing mathematical calculations in Java. Java provides several arithmetic operators to manipulate data values, including addition, subtraction, multiplication, division, and more.

Understanding how to use these operators effectively is a fundamental skill for Java developers.

This tutorial will walk you through Java's arithmetic operators, providing examples to illustrate their usage.

Table of Contents:

1. Introduction to Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, and division. These operators work with primitive data types such as int, float, double, and long.

2. List of Java Arithmetic Operators

Java provides the following arithmetic operators:

Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Modulus (%): Returns the remainder of a division operation.
Java also provides:

Increment (++): Increases a value by 1.
Decrement (–): Decreases a value by 1.

3. Basic Arithmetic Operators with Examples

Addition (+)
The + operator adds two operands together.

Example 1: Addition

public class Main {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        int sum = a + b;
        System.out.println("Sum: " + sum);  // Output: Sum: 30
    }
}

Explanation:

The + operator adds a and b to produce the sum, which is printed.

Subtraction (-)

The – operator subtracts the right operand from the left operand.

Example 2: Subtraction

public class Main {
    public static void main(String[] args) {
        int a = 15;
        int b = 5;
        int difference = a - b;
        System.out.println("Difference: " + difference);  // Output: Difference: 10
    }
}

Multiplication (*)

The * operator multiplies two operands.

Example 3: Multiplication

public class Main {
    public static void main(String[] args) {
        int a = 6;
        int b = 7;
        int product = a * b;
        System.out.println("Product: " + product);  // Output: Product: 42
    }
}

Division (/)

The / operator divides the left operand by the right operand. For integer division, it returns the quotient without the remainder.

Example 4: Division

public class Main {
    public static void main(String[] args) {
        int a = 20;
        int b = 4;
        int quotient = a / b;
        System.out.println("Quotient: " + quotient);  // Output: Quotient: 5
    }
}

Note:

Integer division truncates the decimal part. For example, 7 / 2 will return 3 instead of 3.5.

Modulus (%)

The % operator returns the remainder of a division operation.

Example 5: Modulus

public class Main {
    public static void main(String[] args) {
        int a = 10;
        int b = 3;
        int remainder = a % b;
        System.out.println("Remainder: " + remainder);  // Output: Remainder: 1
    }
}

Explanation:

10 % 3 gives the remainder of the division 10 ÷ 3, which is 1.

4. Increment and Decrement Operators

Increment Operator (++)

The ++ operator increases the value of a variable by 1. There are two types of increment operators:

Pre-increment (++a): Increments a first, then returns the value.
Post-increment (a++): Returns the value of a, then increments it.

Example 6: Pre-increment vs. Post-increment

public class Main {
    public static void main(String[] args) {
        int a = 5;
        
        // Pre-increment
        int pre = ++a;
        System.out.println("Pre-increment: " + pre);  // Output: Pre-increment: 6
        
        // Post-increment
        int post = a++;
        System.out.println("Post-increment: " + post);  // Output: Post-increment: 6
        System.out.println("a after post-increment: " + a);  // Output: a after post-increment: 7
    }
}

Decrement Operator (–)

The — operator decreases the value of a variable by 1. Like the increment operator, there are two types:

Pre-decrement (–a): Decrements a first, then returns the value.
Post-decrement (a–): Returns the value of a, then decrements it.

Example 7: Pre-decrement vs. Post-decrement

public class Main {
    public static void main(String[] args) {
        int b = 10;
        
        // Pre-decrement
        int pre = --b;
        System.out.println("Pre-decrement: " + pre);  // Output: Pre-decrement: 9
        
        // Post-decrement
        int post = b--;
        System.out.println("Post-decrement: " + post);  // Output: Post-decrement: 9
        System.out.println("b after post-decrement: " + b);  // Output: b after post-decrement: 8
    }
}

5. Operator Precedence

In Java, operators follow a precedence hierarchy, which determines the order in which operations are performed. Multiplication, division, and modulus have higher precedence than addition and subtraction.

Example 8: Operator Precedence

public class Main {
    public static void main(String[] args) {
        int result = 10 + 2 * 3;  // Multiplication happens first
        System.out.println("Result: " + result);  // Output: Result: 16
    }
}

Explanation:

2 * 3 is evaluated first because multiplication has higher precedence than addition.
You can override precedence using parentheses ().

Example 9: Overriding Precedence

public class Main {
    public static void main(String[] args) {
        int result = (10 + 2) * 3;  // Addition happens first due to parentheses
        System.out.println("Result: " + result);  // Output: Result: 36
    }
}

6. Type Casting in Arithmetic Operations

When performing arithmetic operations on different data types (e.g., int, double), Java automatically promotes the smaller type to the larger type. You can also cast explicitly.

Example 10: Implicit Type Casting

public class Main {
    public static void main(String[] args) {
        int a = 10;
        double b = 5.5;
        
        double result = a + b;  // 'a' is promoted to double
        System.out.println("Result: " + result);  // Output: Result: 15.5
    }
}

Example 11: Explicit Type Casting

public class Main {
    public static void main(String[] args) {
        double a = 9.7;
        int result = (int) a + 5;  // 'a' is cast to int (truncating the decimal part)
        System.out.println("Result: " + result);  // Output: Result: 14
    }
}

7. Arithmetic Operations with Floating-Point Numbers

Arithmetic operations can also be performed on float and double types. Java handles floating-point arithmetic similarly to integers, but the results are more precise due to the decimal points.

Example 12: Floating-Point Arithmetic

public class Main {
    public static void main(String[] args) {
        double a = 7.5;
        double b = 2.5;

        double sum = a + b;
        double division = a / b;

        System.out.println("Sum: " + sum);          // Output: Sum: 10.0
        System.out.println("Division: " + division);  // Output: Division: 3.0
    }
}

8. Common Use Cases for Arithmetic Operators

Example 13: Calculating Area of a Circle

public class Main {
    public static void main(String[] args) {
        double radius = 5.0;
        double area = Math.PI * radius * radius;  // Area = πr²
        System.out.println("Area of the circle: " + area);
    }
}

Example 14: Calculating Remainder

public class Main {
    public static void main(String[] args) {
        int dividend = 17;
        int divisor = 5;
        int remainder = dividend % divisor;
        System.out.println("Remainder: " + remainder);  // Output: Remainder: 2
    }
}

Conclusion

Java arithmetic operators are essential for performing mathematical operations. From basic operators like addition and subtraction to increment/decrement and modulus, mastering arithmetic operations is a fundamental skill for any Java developer.

Understanding operator precedence and type casting will also help ensure accurate and efficient calculations in your applications.

By following best practices and understanding how these operators work, you can easily manipulate and calculate data in Java.

Related posts

Java Data Types Tutorial

Java Boolean Class Tutorial

Java Number Class Tutorial