The Java Arithmetic operators include operators like Arithmetic Addition, Subtraction, Multiplication, Division, and Modulus. All these Java Arithmetic Operators are binary operators, which means they operate on two operands.

The table below shows all the Arithmetic Operators in Java Programming language with examples.

Java Arithmetic Operators | Operation | Example |
---|---|---|

+ | Addition | 10 + 3 = 13 |

– | Subtraction | 10 – 3 = 7 |

* | Multiplication | 10 * 3 = 30 |

/ | Division | 10 / 3 = 3 |

% | Modulus – It returns the remainder after the division | 10 % 3 = 1 (Here remainder is One). |

**Java Arithmetic Operators Example**

This Java Program allows the user to insert two integer variables a, b. Next, we use these two variables to perform various arithmetic operations in Java Programming Language.

// Program to Perform Arithmetic Operations in Java package JavaOperators; import java.util.Scanner; public class ArithmeticOperators { private static Scanner sc; public static void main(String[] args) { int a, b; int addition, subtraction, multiplication, division, modulus; sc = new Scanner(System.in); System.out.println("\n Please Enter two integer Value: "); a = sc.nextInt(); b = sc.nextInt(); addition = a + b; subtraction = a - b; multiplication = a * b; division = a / b; modulus = a % b; System.out.format("\n Addition of two numbers %d, %d is : %d\n", a, b, addition); System.out.format(" Subtraction of two numbers %d, %d is : %d\n", a, b, subtraction); System.out.format(" Multiplication of two numbers %d, %d is : %d\n", a, b, multiplication); System.out.format(" Division of two numbers %d, %d is : %d\n", a, b, division); System.out.format(" Modulus of two numbers %d, %d is : %d\n", a, b, modulus); } }

When we are using a division ( / ) operator, the result will completely depend upon the data type it belongs to. For example, if the data type is an integer, then it will produce the integer value by rounding the value (7 / 3 = 2). To get the correct result, then change the data type to float. Please do not get confused, let us see one more example for better understanding.

**Java Arithmetic Operators ****using Float**

For this example, We are using two variables a and b, and their values are 25 and 4. We are going to use these two variables to show the problems we generally face while performing Java arithmetic operations on Int and Float Datatype.

// Java Program to Perform Division and Modulus on Float data type package JavaOperators; public class ArithmeticOperatorsUsingFloat { public static void main(String[] args) { int a = 25, b = 4; int integerdiv, modulus; float floatdiv; integerdiv = a / b; modulus = a % b; floatdiv = (float)a / b; System.out.format(" Division of two numbers %d, %d is : %d\n", a, b, integerdiv); System.out.format(" Modulus of two numbers %d, %d is : %d\n", a, b, modulus); System.out.println(" \n---------Correct Results is--------- "); System.out.format(" Division of two numbers %d, %d is : %.3f", a, b, floatdiv); } }

If you observe the above result, we are getting two different results for the same calculation. Because for the first result, both a and b are integers, and the output is also integer (integerdiv). So, the compiler neglects the term after the decimal point and shows answer 6 instead of 6.250, and a % b is 1 because the remainder is 1.

Next, we changed the output data type to float (floatdiv) and also converted the result to float to get our desired result.

**TIP:** Be careful while using the division operator (Typecasting plays a major role here).

## Java Arithmetic Operators using Oops

In this Java Arithmetic Operators example, we are dividing the code using the Object-Oriented Programming. To do this, First, we will create a class that holds methods.

package JavaOperators; public class ArithmeticOperator { int addition, subtraction, multiplication, division, modulus; public int addition(int a, int b) { addition = a + b; return addition; } public int subtraction(int a, int b) { subtraction = a - b; return subtraction; } public int multiplication(int a, int b) { multiplication = a * b; return multiplication; } public int division(int a, int b) { division = a / b; return division; } public int modulus(int a, int b) { modulus = a % b; return modulus; } }

Within the Main program of a Arithmetic Operators example, we will create an instance of the above-specified class and call the methods.

package JavaOperators; import java.util.Scanner; public class ArithmeticOperatorsUsingClass { private static Scanner sc; public static void main(String[] args) { int a, b; int addition, subtraction, multiplication, division, modulus; sc = new Scanner(System.in); System.out.println("\n Please Enter two integer Value: "); a = sc.nextInt(); b = sc.nextInt(); ArithmeticOperator arith = new ArithmeticOperator(); addition = arith.addition(a, b); subtraction = arith.subtraction(a, b); multiplication = arith.multiplication(a, b); division = arith.division(a, b); modulus = arith.modulus(a, b); System.out.format("\n Addition of two numbers %d, %d is : %d\n", a, b, addition); System.out.format(" Subtraction of two numbers %d, %d is : %d\n", a, b, subtraction); System.out.format(" Multiplication of two numbers %d, %d is : %d\n", a, b, multiplication); System.out.format(" Division of two numbers %d, %d is : %d\n", a, b, division); System.out.format(" Modulus of two numbers %d, %d is : %d\n", a, b, modulus); } }

**ArithmeticOperator Class Analysis: **Here, we declared five functions of integer type, and all those methods accept two integer arguments. Within those functions, we are performing Arithmetic operations using Java arithmetic operators.

**Main Class Analysis:**

First, we created an instance / created an Object of the **ArithmeticOperator** Class

ArithmeticOperator arith = new ArithmeticOperator();

Next, we are calling all five methods available in the current Java instance. All those methods return an integer value so, we are assigning the return value to corresponding variables.

addition = arith.addition(a, b); subtraction = arith.subtraction(a, b); multiplication = arith.multiplication(a, b); division = arith.division(a, b); modulus = arith.modulus(a, b);

Lastly, we used the following System.out.format statement to print those arithmetic operators.