Arithmetic Operators in Java

The Java programming language supports various arithmetic operators for all floating-point and integer numbers.

OperatorOperation
+Addition
Subtraction
*Multiplication
/Division
%Modulo Operation (Remainder after division)

1. Addition(+)

This operator is a binary operator and is used to add two operands.

package octopuscodes.com.demo;

public class Demo {

	public static void main(String[] args) {
		int a = 10, b = 5;
		int sum1 = a + b;
		System.out.println("Sum 1: " + sum1);

		float number1 = 5.6f, number2 = 7.8f;
		float sum2 = number1 + number2;
		System.out.println("Sum 2: " + sum2);

		double number3 = 12.3, number4 = 4.31;
		double sum3 = number3 + number4;
		System.out.println("Sum 3: " + sum3);
	}

}
Sum 1: 15
Sum 2: 13.4
Sum 3: 16.61
 

2. Subtraction(-)

This operator is a binary operator and is used to subtract two.

package octopuscodes.com.demo;

public class Demo {

	public static void main(String[] args) {
		int a = 10, b = 5;
		int result1 = a - b;
		System.out.println("Result 1: " + result1);

		float number1 = 5.12f, number2 = 7.33f;
		float result2 = number1 - number2;
		System.out.println("Result 2: " + result2);

		double number3 = 12.35, number4 = 4.31;
		double result3 = number3 - number4;
		System.out.println("Result 3: " + result3);
	}

}
Result 1: 5
Result 2: -2.21
Result 3: 8.04
 

3. Multiplication(*)

This operator is a binary operator and is used to multiply two operands.

package octopuscodes.com.demo;

public class Demo {

	public static void main(String[] args) {
		int a = 10, b = 5;
		int result1 = a * b;
		System.out.println("Result 1: " + result1);

		float number1 = 5.12f, number2 = 7.33f;
		float result2 = number1 * number2;
		System.out.println("Result 2: " + result2);

		double number3 = 12.35, number4 = 4.31;
		double result3 = number3 * number4;
		System.out.println("Result 3: " + result3);
	}

}
Result 1: 50
Result 2: 37.5296
Result 3: 53.2285
 

4. Division(/)

This is a binary operator that is used to divide the first operand(dividend) by the second operand(divisor) and give the quotient as a result

package octopuscodes.com.demo;

public class Demo {

	public static void main(String[] args) {
		int a = 10, b = 5;
		int result1 = a / b;
		System.out.println("Result 1: " + result1);

		float number1 = 5.12f, number2 = 7.33f;
		float result2 = number1 / number2;
		System.out.println("Result 2: " + result2);

		double number3 = 12.35, number4 = 4.31;
		double result3 = number3 / number4;
		System.out.println("Result 3: " + result3);
	}

}
Result 1: 2
Result 2: 0.6984993
Result 3: 2.8372093023255816
 

5. Modulus(%)

This is a binary operator that is used to return the remainder when the first operand(dividend) is divided by the second operand(divisor). 

package octopuscodes.com.demo;

public class Demo {

	public static void main(String[] args) {
		int a = 10, b = 5;
		int result1 = a % b;
		System.out.println("Result 1: " + result1);

		int number1 = 10, number2 = 4;
		int result2 = number1 % number2;
		System.out.println("Result 2: " + result2);
	}

}
Result 1: 0
Result 2: 2

Best Laptops for Programming