Arithmetic Operators in JAVA

Arithmetic Operators in Java provides the ability to perform arithmetic in programming :

  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Modulus
  • Increment
  • 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.

OperatorSymbolExpressionABOutput
Addition+A+B10515
SubtractionA-B1055
Multiplication*A*B10550
Division/A/B1052
Modulus%A%B1050

Arithmetic Operators that require only one operand

Increment and Decrement operators require only one operand to perform their operation as mentioned in the below table.

OperatorExpressionAOutputDescription
Post IncrementA++56The value of A is updated, only after the execution moves to next statement.
Pre Increment++A56The value of A is updated during the execution of this statement itself.
Post DecrementA–43The value of A is updated, only after the execution moves to next statement.
Pre Decrement–A43The value of A is updated during the execution of this statement itself.

Example 1 – Java Arithmetic Operators

Now, let us see the demonstration of arithmetic operators with an Example Java Program, ArithmeticOperatorsExample.java.

Example.java

package com.tutorialkart;

/**
 * @author tutorialkart.com
 */
public class Example {

	public static void main(String[] args) {
		int A=10,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));
		System.out.println("Increment :      (A++) = "+(A++));
		// Value of A gets updated in the next statement, check printing A
		System.out.println("Value of             A = "+A);
		System.out.println("Increment :      (++A) = "+(++A));
		// Value of A gets updated during the execution of same statement
		System.out.println("Decrement :      (A--) = "+(A--));
		// Value of A gets updated in the next statement, check printing A
		System.out.println("Value of             A = "+A);
		// Value of A gets updated during the execution of same statement
		System.out.println("Decrement :      (--A) = "+(--A));
	}
}

Output

Addition :       (A+B) = 15
Subtraction :    (A+B) = 5
Multiplication : (A+B) = 50
Division :       (A+B) = 2
Modulus :        (A+B) = 0
Increment :      (A++) = 10
Value of             A = 11
Increment :      (++A) = 12
Decrement :      (A--) = 12
Value of             A = 11
Decrement :      (--A) = 10

Conclusion

In this Java Tutorial, we have seen available Arithmetic Operators in Java. In our next section of Java Tutorials, we shall learn about Relational Operators in Java.