Arithmetic Operators In Java

Java Arithmetic Operators

As stated earlier, the arithmetic operators are used to perform some arithmetic operations, like addition, subtraction, multiplication, and division. So, I think you might have guessed the symbols too. We are going to use + for addition, – for subtraction, * for multiplication, / for division and % for modulo(calculating remainder after division). Here are the arithmetic operators and their functions listed below –

Arithmetic operator symbol Description
+This symbol is used for performing additional operations.
-4This symbol is used for performing subtraction operations.
*This symbol is used to perform multiplication operations.
/This symbol is used to perform division operations.
%This operator is used to calculate the remainder after the division of two numbers(It is often called as modulus operator).
++This is a unary operator, which adds 1 to the existing value held by some variable. So if we do a++, it means a = a + 1. This means that we are adding 1 to the existing value in a, and storing it back to variable a.
Example: if variable a holds value 5 and we do a++, now the variable will hold value 6.
This is again a unary operator, similar to the previous one, but this one subtracts 1 from the existing value held by some variable. So, if we do a–, it means that a = a – 1. This means that we are subtracting 1 from the existing value of variable a, and then storing it back to variable a.
Example: if a holds value 5, and we do a–, now the variable will hold value 4.

Please refer the program below, which demonstrates the usage of arithmetic operators →

package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Some arithmetic Operations…
int num1 = 20, num2 = 6;
System.out.println(num1 + num2); // 20 + 6 = 26
System.out.println(num1 – num2); // 20 – 6 = 14
System.out.println(num1 * num2); //20 * 6 = 120
System.out.println(num1 / num2); // 20 / 6 = 3 (being an int, the numbers after point not taken)
//the output for the above line is 3 because of integer division.
//if data was float type, the output would have been 3.333333 (try doing float instead of int)
System.out.println(num1 % num2); // this gives the remainder after division. 2 in this case.
num1++; // incrementing num1. this is same as num1 = num1 + 1;
System.out.println(num1);
num2–; // decrementing num2. this is same as num2 = num2 – 1
System.out.println(num2);
}
}

So, if you try executing the above program, you get some output like the following –

26
14
120
3
2
21
5

So, as you can see that we can use these arithmetic operators as and when needed. Well, this was a kind of a basic thing but is good to start with. For example, if you are supposed to write a program where you want to check whether the number is odd or even, then you would use the modulo (%) operator, with which we get the remainder after the division. So all you will be doing is try dividing the required number by 2 and check if the remainder is zero. If the remainder is zero, you can simply say that the number is even, else the number is odd. So, this was one of the simple use cases of arithmetic operators. Other than this, you can use them for some mathematical operations like addition, subtraction, multiplication, and division.