Thanks to visit codestin.com
Credit goes to www.rameshfadatare.com

Java Arithmetic Operators

Introduction

Java provides a set of arithmetic operators that are used to perform common mathematical operations such as addition, subtraction, multiplication, division, and modulus. These operators work on numeric data types like int, float, double, and long. Understanding how to use these operators effectively is fundamental to performing calculations and manipulating numerical data in Java.

Key Points:

  • Basic Operations: Arithmetic operators perform essential mathematical calculations.
  • Works on Numeric Types: Operate on data types such as int, float, double, and long.
  • Operator Precedence: Arithmetic operations follow a specific order of precedence, which determines the sequence in which operations are performed.

List of Arithmetic Operators

Java provides the following arithmetic operators:

Operator Description Example Output
+ Addition a + b Sum of a and b
- Subtraction a - b Difference between a and b
* Multiplication a * b Product of a and b
/ Division a / b Quotient of a divided by b
% Modulus (Remainder) a % b Remainder of a divided by b

Detailed Examples of Arithmetic Operators

Let’s explore each arithmetic operator with examples.

1. Addition (+)

The + operator adds two operands.

public class AdditionExample {
    public static void main(String[] args) {
        int a = 10;
        int b = 5;
        int sum = a + b;

        System.out.println("Sum: " + sum);  // Output: Sum: 15
    }
}

Explanation:

  • Operands: a and b are the operands, and sum stores the result of their addition.

Output:

Sum: 15

2. Subtraction (-)

The - operator subtracts the second operand from the first.

public class SubtractionExample {
    public static void main(String[] args) {
        int a = 10;
        int b = 5;
        int difference = a - b;

        System.out.println("Difference: " + difference);  // Output: Difference: 5
    }
}

Explanation:

  • Operands: a and b are the operands, and difference stores the result of their subtraction.

Output:

Difference: 5

3. Multiplication (*)

The * operator multiplies two operands.

public class MultiplicationExample {
    public static void main(String[] args) {
        int a = 10;
        int b = 5;
        int product = a * b;

        System.out.println("Product: " + product);  // Output: Product: 50
    }
}

Explanation:

  • Operands: a and b are multiplied, and the result is stored in product.

Output:

Product: 50

4. Division (/)

The / operator divides the first operand by the second.

public class DivisionExample {
    public static void main(String[] args) {
        int a = 10;
        int b = 5;
        int quotient = a / b;

        System.out.println("Quotient: " + quotient);  // Output: Quotient: 2
    }
}

Explanation:

  • Operands: a is divided by b, and the result is stored in quotient.
  • Note: If both operands are integers, the result will be an integer (truncating any fractional part).

Output:

Quotient: 2

5. Modulus (%)

The % operator returns the remainder of the division of the first operand by the second.

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
    }
}

Explanation:

  • Operands: a is divided by b, and the remainder of this division is stored in remainder.

Output:

Remainder: 1

Conclusion

Java’s arithmetic operators are fundamental to performing basic mathematical operations in programming. These operators are essential for manipulating numerical data and are commonly used in various computational tasks.

Summary:

  • Addition (+): Adds two operands.
  • Subtraction (-): Subtracts the second operand from the first.
  • Multiplication (*): Multiplies two operands.
  • Division (/): Divides the first operand by the second.
  • Modulus (%): Returns the remainder of the division of the first operand by the second.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top