Java Arithmetic Operators with Examples

Last Updated : 9 Jun, 2026

Arithmetic operators in Java are used to perform mathematical calculations on numeric values. They are among the most commonly used operators in programming and help perform operations such as addition, subtraction, multiplication, division, and finding remainders. These operators work with Java's primitive numeric data types like int, float, double, long, and others.

  • Used to perform mathematical calculations on operands.
  • Can be applied to integer and floating-point data types.
  • Include both unary and binary operators.

Arithmetic Operators in Java

Now let's look at each one of the arithmetic operators in Java: 

1. Addition(+)

The addition operator is a binary operator that adds two operands and returns their sum.

Syntax: 

operand1 + operand2

Java
import java.io.*;

class Addition {
    public static void main(String[] args)
    {
        // initializing variables
        int num1 = 10, num2 = 20, sum = 0;

        // Displaying num1 and num2
        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);

        // adding num1 and num2
        sum = num1 + num2;
        System.out.println("The sum = " + sum);
    }
}

Output
num1 = 10
num2 = 20
The sum = 30

2. Subtraction(-)

The subtraction operator is a binary operator that subtracts the second operand from the first operand.

Syntax: 

operand1 - operand2

Java
import java.io.*;

class Subtraction {
    public static void main(String[] args)
    {
        // initializing variables
        int num1 = 20, num2 = 10, sub = 0;

        // Displaying num1 and num2
        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);

        // subtracting num1 and num2
        sub = num1 - num2;
        System.out.println("Subtraction = " + sub);
    }
}

Output
num1 = 20
num2 = 10
Subtraction = 10

3. Multiplication(*)

 The multiplication operator is a binary operator that multiplies two operands and returns their product.

Syntax: 

operand1 * operand2

Java
import java.io.*;

class Multiplication {
    public static void main(String[] args)
    {
        // initializing variables
        int num1 = 20, num2 = 10, mult = 0;

        // Displaying num1 and num2
        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);

        // Multiplying num1 and num2
        mult = num1 * num2;
        System.out.println("Multiplication = " + mult);
    }
}

Output
num1 = 20
num2 = 10
Multiplication = 200

4. Division(/)

The division operator is a binary operator that divides the first operand by the second operand and returns the quotient.

Syntax: 

operand1 / operand2

Java
import java.io.*;

class Division {
    public static void main(String[] args)
    {
        // initializing variables
        int num1 = 20, num2 = 10, div = 0;

        // Displaying num1 and num2
        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);

        // Dividing num1 and num2
        div = num1 / num2;
        System.out.println("Division = " + div);
    }
}

Output
num1 = 20
num2 = 10
Division = 2

5. Modulus(%) 

The modulus operator is a binary operator that returns the remainder after dividing the first operand by the second operand.

Syntax: 

operand1 % operand2

Java
import java.io.*;

class Modulus {
    public static void main(String[] args)
    {
        // initializing variables
        int num1 = 5, num2 = 2, mod = 0;

        // Displaying num1 and num2
        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);

        // Remaindering num1 and num2
        mod = num1 % num2;
        System.out.println("Remainder = " + mod);
    }
}

Output
num1 = 5
num2 = 2
Remainder = 1

Example: Program in Java that implements all basic arithmetic operators for user input:

Java
import java.util.Scanner;

public class ArithmeticOperators {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the first number: ");
        double num1 = sc.nextDouble();

        System.out.print("Enter the second number: ");
        double num2 = sc.nextDouble();

        double sum = num1 + num2;
        double difference = num1 - num2;
        double product = num1 * num2;
        double quotient = num1 / num2;

        System.out.println("The sum of the two numbers is: " + sum);
        System.out.println("The difference of the two numbers is: " + difference);
        System.out.println("The product of the two numbers is: " + product);
        System.out.println("The quotient of the two numbers is: " + quotient);
    }
}

Input

Enter the first number: 20
Enter the second number: 10

Output

The sum of the two numbers is: 30.0
The difference of the two numbers is: 10.0
The product of the two numbers is: 200.0
The quotient of the two numbers is: 2.0

Explanation: This program uses the Scanner class to take two numbers as input from the user. It reads the values using nextDouble(), performs basic arithmetic operations such as addition, subtraction, multiplication, and division, and stores the results in separate variables. Finally, it displays the calculated results using System.out.println(). This demonstrates how arithmetic operators can be used with user input in Java.

Comment