Java Arithmetic Operators with Examples
Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they provide. Here are a few types:
- Arithmetic Operators
- Unary Operators
- Assignment Operator
- Relational Operators
- Logical Operators
- Ternary Operator
- Bitwise Operators
- Shift Operators
This article explains all that one needs to know regarding Arithmetic Operators.
These operators involve the mathematical operators that can be used to perform various simple or advanced arithmetic operations on the primitive data types referred to as the operands. These operators consist of various unary and binary operators that can be applied on a single or two operands. Let’s look at the various operators that Java has to provide under the arithmetic operators.
Now let’s look at each one of the arithmetic operators in Java:
1. Addition(+): This operator is a binary operator and is used to add two operands.
num1 + num2
num1 = 10, num2 = 20 sum = num1 + num2 = 30
num1 = 10 num2 = 20 The sum = 30
2. Subtraction(-): This operator is a binary operator and is used to subtract two operands.
num1 - num2
num1 = 20, num2 = 10 sub = num1 - num2 = 10
num1 = 20 num2 = 10 Subtraction = 10
3. Multiplication(*): This operator is a binary operator and is used to multiply two operands.
num1 * num2
num1 = 20, num2 = 10 mult = num1 * num2 = 200
num1 = 20 num2 = 10 Multiplication = 200
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.
num1 / num2
num1 = 20, num2 = 10 div = num1 / num2 = 2
num1 = 20 num2 = 10 Division = 2
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).
num1 % num2
num1 = 5, num2 = 2 mod = num1 % num2 = 1
num1 = 5 num2 = 2 Remainder = 1