Java Arithmetic Operators
h:sptt//www.theitroad.com
Java arithmetic operators are used to perform mathematical operations on numerical data types. The following are the arithmetic operators in Java:
- Addition:
+
The addition operator is used to add two or more numerical values.
int a = 10; int b = 5; int sum = a + b; // sum is 15
- Subtraction:
-
The subtraction operator is used to subtract one numerical value from another.
int a = 10; int b = 5; int difference = a - b; // difference is 5
- Multiplication:
*
The multiplication operator is used to multiply two or more numerical values.
int a = 10; int b = 5; int product = a * b; // product is 50
- Division:
/
The division operator is used to divide one numerical value by another.
int a = 10; int b = 5; int quotient = a / b; // quotient is 2
Note that if the division operator is used with two integers, the result will be an integer. If the result of the division is a decimal value, it will be truncated to an integer value.
- Modulus:
%
The modulus operator is used to find the remainder of a division operation.
int a = 10; int b = 3; int remainder = a % b; // remainder is 1
The modulus operator is often used to determine whether a number is even or odd. If a number is divisible by 2, the remainder will be 0, indicating that the number is even. If the remainder is 1, the number is odd.