Java
Operators in Java
←Operators
Arithmetic Operators
17
#description
In Java, arithmetic operators are used to perform mathematical computations.
The following are the arithmetic operators in Java:
- Addition (+): Adds two operands.
Example:
int a = 5; int b = 3; int sum = a + b; System.out.println("The sum of a and b is " + sum);
Output:
The sum of a and b is 8
- Subtraction (-): Subtracts second operand from first.
Example:
int a = 5; int b = 3; int diff = a - b; System.out.println("The difference of a and b is " + diff);
Output:
The difference of a and b is 2
- Multiplication (*): Multiplies two operands.
Example:
int a = 5; int b = 3; int product = a * b; System.out.println("The product of a and b is " + product);
Output:
The product of a and b is 15
- Division (/): Divides first operand by second.
Example:
int a = 5; int b = 3; int quotient = a / b; System.out.println("The quotient of a and b is " + quotient);
Output:
The quotient of a and b is 1
- Modulus (%): Gives remainder of first operand divided by second.
Example:
int a = 5; int b = 3; int remainder = a % b; System.out.println("The remainder of a and b is " + remainder);
Output:
The remainder of a and b is 2
Note: When performing arithmetic operations with different data types, the result will be of the data type with the greatest precision. For example, when dividing an integer by a double, the result will be a double.
March 24, 2023