Arithmetic Operators of Java

arithmetic operators in java

Arithmetic operators in Java are symbols used to perform basic mathematical operations like addition, subtraction, multiplication, division and modulus. They help programmers to perform calculations easily and handle numeric data effectively within programs.

    Arithmetic operators in Java are used to perform basic mathematical operations — just like we do in our daily life when we add, subtract, multiply, or divide numbers. These operators help in performing calculations between variables and constants easily.

Purpose of Arithmetic Operators

    The main purpose of arithmetic operators is to help programmers perform mathematical calculations easily within a program. They are commonly used in loops, formulas, data processing, and decision-making statements.

Why Arithmetic Operators Matter in Java?

    Arithmetic operators are the foundation of all programming calculations. Whether you are creating a calculator app, analyzing data, or building a game score counter – arithmetic operators make it possible to compute and display correct results. Without them, handling numbers in a program would be nearly impossible.

Types of Arithmetic Operators in Java

Java provides five main arithmetic operators, which are:

  • Addition (+) – Adds two values.
  • Subtraction (-) – Subtracts one value from another.
  • Multiplication (*) – Multiplies two values.
  • Division (/) – Divides one value by another.
  • Modulus (%) – Finds the remainder after division.

Arithmetic Operators in Detail

1. Addition (+)

    This operator adds two values together. For example:

int sum = 10 + 5;  // Result: 15

    Here, 10 and 5 are added to give 15.

 2. Subtraction (-)

    This operator subtracts one value from another. For example:

int difference = 10 - 5;  // Result: 5

    It finds the difference between two numbers.

3. Multiplication (*)

    This operator multiplies two numbers. For example:

int product = 10 * 5;  // Result: 50

    It’s used when we need repeated addition or total values.

4. Division (/)

    This operator divides one number by another and gives the quotient. For example:

int result = 10 / 5;  // Result: 2

    It’s important to note that if both numbers are integers, the result will also be an integer (without any decimal part).

5. Modulus (%)

    This operator gives the remainder after division. For example:

int remainder = 10 % 3;  // Result: 1

    It is useful when we need to check whether a number is even or odd or when working with repetitive patterns. 

Example Program:

class ArithmeticExample {
  public static void main(String args[]) {
    int a = 10, b = 5;
    System.out.println("Addition: " + (a + b));
    System.out.println("Subtraction: " + (a - b));
    System.out.println("Multiplication: " + (a * b));
    System.out.println("Division: " + (a / b));
    System.out.println("Remainder: " + (a % b));
  }
}

Precedence and Associativity of Arithmetic Operators

    When we perform mathematical operations, the order in which the operations are carried out can completely change the result. For example:

10 + 5 * 2 can be 20 or 30, depending on which operation is done first!

    To avoid confusion, Java follows fixed rules known as operator precedence and associativity.

What is Operator Precedence?

    Operator precedence means the priority or order in which operators are executed when an expression has more than one operator. Operators with higher precedence are evaluated before those with lower precedence.

    Just like in regular math, multiplication and division are done before addition and subtraction. For example:

int result = 10 + 5 * 2;

    Here, * (multiplication) has a higher precedence than + (addition), so Java first calculates 5 * 2 = 10, then adds 10 → 10 + 10 = 20.

What is Associativity?

    Associativity tells us the direction in which operators of the same precedence are evaluated. Most arithmetic operators in Java are left-to-right associative, which means Java performs operations from left to right when operators have the same precedence. For example:

int result = 20 - 10 - 5;

    Here, both – operators have the same precedence, so Java calculates from left to right → (20 – 10) – 5 = 5.

Order of Precedence for Arithmetic Operators

    The order in which arithmetic operators are executed (from highest to lowest priority):

  • Multiplication (*), Division (/), and Modulus (%) → Higher precedence
  • Addition (+) and Subtraction (-) → Lower precedence

So, if an expression has both multiplication and addition, multiplication happens first.

Final Thought on Operators

    Arithmetic operators are the building blocks of all calculations in Java. They make it easy to perform mathematical tasks and handle numeric data efficiently. Mastering these operators is essential for every Java beginner, as they appear in almost every program you’ll ever write.


Share the Post:

Leave a Comment

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

Related Posts​

rational numbers class 7 selina

Rational Numbers

Step by Step solutions of Concise Mathematics ICSE Class-7 Maths chapter 2- Rational Numbers by...
Read More
identifiers in java

Identifiers in Java

Identifiers in Java are just names that label variables, methods and classes which make code...
Read More

Join Our Newsletter

Name
Email
The form has been submitted successfully!
There has been some error while submitting the form. Please verify all form fields again.
Scroll to Top