Arithmetic Operators in Java
Java Arithmetic Operators in provides the ability to perform Mathematical Arithmetic operations in Java programming.
The following are the list of Arithmetic Operations supported in Java.
- Addition
- Subtraction
- Multiplication
- Division
- Modulus
- Increment (Pre-Increment, Post-Increment)
- Decrement (Pre-Decrement, Post-Decrement)
Arithmetic Operators that require two operands
Out of the seven arithmetic operators, that java provides, the operators mentioned in the below table require two operands to perform the arithmetic operation.
ArithmeticOperation | OperatorSymbol | Example | Description |
---|---|---|---|
Addition | + | a + b | Returns sum of a and b. |
Subtraction | - | a – b | Returns difference of b from a. |
Multiplication | * | a * b | Returns product of a and b. |
Division | / | a / b | Returns the quotient when a is divided by b. |
Modular Division | % | a % b | Returns reminder when a is divided by b. |
Increment | ++ | a++ is Post-Increment.++a is Pre-Increment. | Increments the value of a by one.Post-Increment: The value of a is updated, only after the execution moves to next statement.Pre-Increment: The value of a is updated during the execution of this statement itself. |
Decrement | -- | a-- is Post-Decrement.--a is Pre-Decrement. | Decrements the value of a by one.Post-Decrement: The value of a is updated, only after the execution moves to next statement.Pre-Decrement: The value of a is updated during the execution of this statement itself. |
Example
Now, let us see the demonstration of arithmetic operators with an Example Java Program, ArithmeticOperatorsExample.java.
Main.java
public class Main { public static void main(String[] args) { int a = 10; int b = 5; 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 / b)); System.out.println("Modulus : (a % b) = " + (a % b)); a = 10; System.out.println("During Post-Increment : a = "+(a++)); System.out.println("After Post-Increment : a = " + a); a = 10; System.out.println("During Pre-Increment : a = " + (++a)); System.out.println("After Pre-Increment : a = " + a); a = 10; System.out.println("During Post-Decrement : a = "+(a--)); System.out.println("After Post-Decrement : a = " + a); a = 10; System.out.println("During Pre-Decrement : a = " + (--a)); System.out.println("After Pre-Decrement : a = " + a); } }
Output
ddition : (a + b) = 15 Subtraction : (a - b) = 5 Multiplication : (a * b) = 50 Division : (a / b) = 2 Modulus : (a % b) = 0 During Post-Increment : a = 10 After Post-Increment : a = 11 During Pre-Increment : a = 11 After Pre-Increment : a = 11 During Post-Decrement : a = 10 After Post-Decrement : a = 9 During Pre-Decrement : a = 9 After Pre-Decrement : a = 9
Java Arithmetic Operator Tutorials
The following tutorials provide a well detailed explanation and examples for each of the Arithmetic Operators in Java.
- Java Addition
- Java Subtraction
- Java Multiplication
- Java Division
- Java Modulus
- Java Increment
- Java Decrement
Conclusion
In this Java Tutorial, we learned about different Arithmetic Operators in Java.