Java Arithmetic Operators
Operators are fundamental components of programming languages, facilitating various calculations and functions. In Java, operators are diverse, serving purposes like arithmetic, logical, relational, and more. They include:
Arithmetic Operators
Arithmetic Operators in Java are nothing but mathematical operators, which are used to implement simple or advanced-level calculations on all the primitive data types including integers, floats, and even characters (except the boolean data type).
Arithmetic operators in Java can be divided into two categories: Binary Operators and Unary Operators.
- Binary Operators - The operators that require two operands to implement the action/operation are known as Binary operators.
- Unary Operators - The operators who perform the operation on a single variable and provide results are known as Unary Operators.
Operator Name | Category | Operator | Description |
---|---|---|---|
Addition Operator | Binary | + | Addition of two numbers |
Subtraction Operator | Binary | - | Subtraction of two numbers |
Multiplication Operator | Binary | * | Multiplication of two numbers |
Division Operator | Binary | / | Division of two numbers |
Modulus Operator | Binary | % | Return remainder after dividing two numbers |
Increment Operator | Unary | ++ | Increase the value by 1 |
Decrement Operator | Unary | - - | Decrease the value by 1 |
1. Addition Operator (+)
The addition operator is a binary operator as it is used to perform the addition of two operands. (Operand is a primitive data type variable)
Syntax -
Example
Example Program:
Output-
2. Subtraction Operator (-)
The subtraction operator implements the subtraction operation as we do in mathematics. This binary operator can only deal with numeric values.
Syntax -
Example
Example Program:
Output-
3. Multiplication Operator (*)
As we do in mathematics, the Multiplication operator in Java is used to implement the multiplication of two operands. This binary operator executes the basic and advanced level mathematic multiplication.
The multiplication operator can be used with int, float, double, and also with char. But with the char data type, multiplication is performed with the character's ASCII values.
Syntax -
Example
Example Program:
Output-
4. Division Operator (/)
The division operator is a binary operator which uses two operands (dividend and divisor) and gives the result (quotient) by dividing them. We can use division operators with primitive data types int, float, double, also char, wherewith char division is performed on their ASCII value.
If both dividend and divisor are a type of integer then the quotient is also an integer. Moreover, if any of them has a data type of float or double then the result will have a decimal value.
Syntax -
Example
Example Program:
Output:
Division Operator also causes one type of exception. When we are dividing any value with 0 (zero) the system throws a DivideByZeroException, which is a type of an Arithmetic Exception.
Output -
5. Modulus Operator (%)
In Java's arithmetic operator, modulo is a very helpful operator. This binary provides the remainder when the dividend is divided by the divisor.
We can use Modulus operators with primitive data types: int, float, double, also char. Moreover, with a negative value also modulo works the same except it shows a negative result.
Syntax -
Example
Example Program:
Output -
Like the Divison operator, Modulus also throws an ArithmaticException and returns NaN when we take the modulo of any value with 0.
Output -
6. Increment Operator (++)
Unlike the above-discussed operators, this is a unary operator, that can use on a single operand. The increment operator increases the variable value by 1.
Moreover, it can be achieved using two different ways:
- Post Increment Operator: When the increment operator is placed after the operands, the actual value of the variable is used first, and then it gets incremented.
Syntax-
Example
- Pre Increment Operator: When the increment operator is placed before the operands, the value of the variable increment is first and then used.
Syntax
Example
Example Program:
Output -
7. Decrement Operator (--)
A decrement operator is a unary operator which acts on one operand. This operator decreases the integer value by 1 at a time.
Like the Increment operator, the decrement operator also can be used in two different ways:
- Post Decrement Operator: When the decrement operator is placed after the operands, the actual value of the variable is used first, and then it gets decremented.
Syntax
Example
- Pre Decrement Operator: When the decrement operator is placed before the operands, the value of the variable decrement is first and then used.
Syntax
Example
Example Program:
Output -
8. Operators for Compound Assignments
Compound assignment operators in Java allow performing a computation and assigning the result to a variable in a single statement. By combining an arithmetic operator with an assignment operator, these operators streamline code and enhance readability. Various varieties include:
- +=: Adds the value on the right-hand side (RHS) to the variable on the left-hand side (LHS).
- -=: Subtracts the value on the RHS from the variable on the LHS.
- *=: Multiplies the variable on the LHS by the value on the RHS.
- /=: Divides the variable on the LHS by the value on the RHS.
- %=: Computes the remainder of dividing the variable on the LHS by the value on the RHS.
Below is a program demonstrating the implementation of all basic arithmetic operators in Java for user input:
Output -
The above code is the same as the last one except, that here we have used the Scanner class. Scanner class is in util package and used to take input from users. So instead of assigning value directly, we do not take it from the user. You can see that for int and float we used different styles nextInt() and nextFloat() respectively.
Conclusion
- Arithmetic operators in Java perform mathematical calculations like addition, subtraction, multiplication, division, and modulus.
- Arithmetic operators in Java can be categorized into binary operators, which require two operands, and unary operators, which operate on a single operand.
- Binary Operators are Addition (+), Subtraction (-), Multiplication (*), Division (/), and Modulus (%).
- Unary Operators are Increment (++) and Decrement (--), which respectively increase and decrease the value of a variable by 1.
- Compound Assignment operators combine arithmetic operations with assignment, enabling concise expressions like +=, -=, *=, /=, and %= for addition, subtraction, multiplication, division, and modulus, respectively.