BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Arithmetic Operators in Java with Examples

Last Updated: October 15, 2022 by Chaitanya Singh | Filed Under: java

Operator is a symbol that instructs the compiler to perform a specific action. For example, a “+” operator instructs the compiler to perform addition, a “>” operator instructs the compiler to perform comparison, “=” for assignment and so on. The operators in java are classified in eight different categories. In this guide, we will mainly discuss Arithmetic operators in Java.

In any operation, there is an operator and operands. For example: In a+b, the “+” symbol is the operator and a & b are operands.

Arithmetic Operators in Java

Arithmetic Operators List in Java

The five arithmetic operators in Java are: + (addition), – (subtraction), * (multiplication), / (division), and % (modulus) represented in the following list:

1. + Operator Example

The addition operator, represented by symbol + is used for adding two operands. In the following example, we are adding two integer numbers using + operator.

class JavaExample {
  public static void main(String[] args) {
    //declaring integer variables
    int x = 100, y = 20, add;

    add = x+y;

    System.out.println("Sum of x and y is: "+add);
  }
}

Output:

Sum of x and y is: 120

In the above example, both the variables x and y are integers, so the type of variable add is declared int.

However if one or both variables are either float or double then the type of add cannot be int. For example:

class JavaExample {
  public static void main(String[] args) {
    int x = 100;

    //y is float type so the variable 'add' is
    //declared as float type to hold the addition result
    float y = 20.5f, add;

    //addition of int and float. The result is a float
    add = x+y;

    System.out.println("Sum of x and y is: "+add);
  }
}

Output:

Output sum of int and float

2. – Operator Example

The -(minus) arithmetic operator returns the subtraction result of two operands. In the following example, we have two variables x and y are we are subtracting the value of y from x, the result is stored in the variable subtraction.

class JavaExample {
  public static void main(String[] args) {
    int x = 100, y = 20, subtraction;

    //The value of y is subtracted from value of x and
    //the result is stored in a variable subtraction
    subtraction = x-y;

    System.out.println("The value of x - y is: "+subtraction);
  }
}

Output:

The value of x - y is: 80

Note: The important point to note here is that while we are saying that we are subtracting the y from x, however the values of these variables x and y remains unchanged.

Let’s print the value of both the variables after performing an arithmetic operation to see whether is there any change.

class JavaExample {
  public static void main(String[] args) {
    int x = 100, y = 20, subtraction;

    subtraction = x-y;

    System.out.println("Subtraction result: "+subtraction);

    //x and y remains unchanged after any arithmetic operation
    System.out.println("x: "+x);
    System.out.println("y: "+y);
  }
}

Output:

Operands value remains unchanged after arithmetic operaton

3. * Operator Example

class JavaExample {
  public static void main(String[] args) {
    int x = 100, y = 20, multiply;
    multiply = x*y;
    System.out.println("Multiplication of x and y is: "+multiply);
  }
}

Output:

Multiplication of x and y is: 2000

In the above example, the variables x and y are integers, this is why the variable multiply that holds the multiplication result is int type. However, if any of the variable is float or double then the variable, where the multiplication result is stored must not be integer. Let’s look at the following example:

class JavaExample {
  public static void main(String[] args) {
    int x = 100, multiply;
    float y = 5.5f;
    multiply = x*y;
    System.out.println("Multiplication of x and y is: "+multiply);
  }
}

Output: Compilation error.

Error incompatible types

Error can be fixed by changing the type of multiply variable. A float type variable can hold the product of int and float variables.

class JavaExample {
  public static void main(String[] args) {
    int x = 100;
    float y = 5.5f, multiply;
    multiply = x*y;
    System.out.println("Multiplication of x and y is: "+multiply);
  }
}

Output:

Multiplication of x and y is: 550.0

4. / Operator Example

The / operator returns the quotient after dividing an operand by another operand. For example, ‘/’ operator would return 2, if 100 is divided by 50 (100/50 == 2), however the % modulus operator would return 0 for the same operation as it returns remainder.

class JavaExample {
  public static void main(String[] args) {
    int x = 100, y = 20, division;

    //The '/' arithmetic operator divides the variable x by
    //variable y and returns the quotient
    division = x/y;
    System.out.println("Quotient value after dividing x by y: "+division);
  }
}

Output:

Quotient value after dividing x by y: 5

Similar to the multiplication example, if any of the variable is of float or double type then the variable, where the result is stored must have the float or double data type.

class JavaExample {
  public static void main(String[] args) {
    int x = 100;

    float y = .25f, division;

    //In this arithmetic operation, one of the variable is of float
    //type so the 'division' variable is declared 'float' type.
    division = x/y;
    System.out.println("Quotient value after dividing x by y: "+division);
  }
}

Output:

Quotient value after dividing x by y: 400.0

5. % Operator Example

class JavaExample {
  public static void main(String[] args) {
    int x = 100, y = 20, mod;

    //The modulus operator '%' returns the remainder after division
    //In this case, the value 100 is perfectly divisible by value 20
    //thus the modulus is 0 here as no remainder.
    mod = x % y;
    System.out.println("Remainder value after dividing x by y: "+mod);
  }
}

Output:

output modulus operator

Recommended Posts

  • Assignment Operators in Java with Examples
  • Unary Operators in Java with Examples
  • Logical Operators in Java with Examples
  • Relational Operators in Java with Examples
  • Bitwise Operators in Java with Examples
  • Ternary Operator in Java with Examples
  • Shift Operators in Java with Examples
❮ Operators in Java

Top Related Articles:

  1. Java Variables: Declaration, Scope, and Naming Conventions
  2. Can Static Methods be Overloaded or Overridden in Java?
  3. Unary Operators in Java with Examples
  4. Logical Operators in Java with Examples
  5. Relational Operators in Java with Examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap