The way we calculate mathematical calculations, in the same way, Java provides arithmetic operators for mathematical operations. It provides operators for all necessary mathematical calculations.
Basic Arithmetic Operators
There are various arithmetic operators used in Java:
Operator | Meaning | Work |
---|---|---|
+ | Addition | To add two operands. |
- | Subtraction | To subtract two operands. |
* | Multiplication | To multiply two operands. |
/ | Division | To divide two operands. |
% | Modulus | To get the area of the division of two operands. |
Arithmetic operators are applied on integer and floating-point and not on boolean types. But you can use them on the characters' because, in Java, Char is sub-set of the integer.
Program to Show Arithmetic Operators Works
Example:
public class arithop {
public static void main(String[] args) {
//Variables Definition and Initialization
int number1 = 12, number2 = 4;
//Addition Operation
int sum = number1 + number2;
System.out.println("Sum is: " + sum);
//Subtraction Operation
int dif = number1 - number2;
System.out.println("Difference is : " + dif);
//Multiplication Operation
int mul = number1 * number2;
System.out.println("Multiplied value is : " + mul);
//Division Operation
int div = number1 / number2;
System.out.println("Quotient is : " + div);
//Modulus Operation
int rem = number1 % number2;
System.out.println("Remainder is : " + rem);
}
}
Output:
Sum is: 16 Difference is : 8 Multiplied value is : 48 Quotient is : 3 Remainder is : 0