Java - Arithmetic Operators
Java supports different arithmetic operators that can be used to performs mathematical calculations on the values and variables aka operands.
The basic arithmetic operations includes addition, subtraction, multiplication, division, etc. are performed with these operators.
Arithmetic operations are performed according to the order or precedence of operators.
The general structure of every expression
Operand Operator
Operand [Operator
Operand] …
- Operand represents data.
- Every expression will follow this general structure.
- A symbol used to perform operation such as mathematical, logical or relational producing a resultant output is known as an operator.
- For example,
a + b
wherea
andb
are operands and+
is an operator. - For example,
costOfPen = 7
where,=
is an operator andcostOfPen
and7
are operands.
In this article, you will find Arithmetic operators provided by Java.
Arithmetic operators
Basic mathematical calculations are preformed on numeric values with the use of arithmetic operators.
Operator | Description | Example |
---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
-expr | Unary minus, aka negation (reverse the sign of the expression) | -a |
* | Multiplication | a * b |
/ | Division | a / b |
% | Get the remainder of an integer division (modulo) | a % b |
- All of these listed operators are binary operators.
- All these operators also follow the general structure of
Operand
Operator
Operand
, meaning that an operator is always surrounded by two operands. - For example, an expression
a + b
is a binary operation, where a and b are the two operands and + is an operator.
class ArithmeticOperatorsDemo
{
public static void main(String[] args)
{
// Arithmetic Operators
int a = 8, b = 6;
System.out.println("Value of A : " + a);
System.out.println("Value of B : " + b);
int sum = a + b;
System.out.println("Addition : " + sum);
System.out.println("Addition a+b : " + a + b);
System.out.println("Addition(a+b) : " + (a + b));
System.out.println("Subtraction(a-b) : " + (a - b));
System.out.println("Multiplication(a*b) : " + (a * b));
System.out.println("Division(a/b) : " + (a / 10));
System.out.println("Remainder(a%b) : " + (a % b));
System.out.println("------------------------------");
double c = 18, d = 7;
System.out.println("Value of A : " + c);
System.out.println("Value of B : " + d);
double add = c + d;
System.out.println("Addition : " + add);
System.out.println("Addition c+d : " + c + d);
System.out.println("Addition(c+d) : " + (c + d));
System.out.println("Subtraction(c-d) : " + (c - d));
System.out.println("Multiplication(c*d) : " + (c * d));
System.out.println("Division(c/d) : " + (c / d));
System.out.println("Remainder(c%d) : " + (c % d));
}
}
Value of A : 8
Value of B : 6
Addition : 14
Addition a+b : 86
Addition(a+b) : 14
Subtraction(a-b) : 2
Multiplication(a*b) : 48
Division(a/b) : 0
Remainder(a%b) : 2
------------------------------
Value of A : 18.0
Value of B : 7.0
Addition : 25.0
Addition c+d : 18.07.0
Addition(c+d) : 25.0
Subtraction(c-d) : 11.0
Multiplication(c*d) : 126.0
Division(c/d) : 2.5714285714285716
Remainder(c%d) : 4.0
Hope you like this!
Keep helping and happy 😄 coding