# Arithmetic Operators

- Eclipse: Oxygen
- Java: 1.8

Java provides a rich set of operators to manipulate a variable. Operators are the symbols in Java that perform a specific operation on many operands and returns the output. Before knowing the arithmetic operators, let us know the type of operator.

Java operators are divided into the following groups:

- Arithmetic Operators
- Assignment Operators
- Bitwise Operators
- Logical Operators
- Relational Operators
- Operators

**Arithmetic operator**

In this tutorial, we will discuss the arithmetic operator. Mathematical operations are done by using arithmetic operators. Following table displays all the arithmetic operators supported by java. Assume integer variable “A” holds value 5 and variable “B” holds value 10.

Operator |
Description |
Example |
Type Of Operation |

+ | Add two operands | A+B will give 15 | Binary |

– | Subtract the second operand from first | A-B will give -5 | Binary |

* | Multiply both operands | A*B will give 50 | Binary |

/ | Divide numerator by de-numerator | B/A will give 2 | Binary |

% | Modulus operator gives a reminder after an integer division | B%A will give 0 | Binary |

++ | Increment operator increases integer value by one | A++ will give 6 | Unary |

— | Decrement operator decreases integer value by one | A—will give 4 | Unary |

**Addition Operator**

Plus (+) operator is used for addition of two numbers. In addition to adding two numbers, the addition operator is also used for string concatenation.

The following code demonstrates the addition of two numbers as well as concatenation operation.

1 2 3 4 5 6 |
public static void main(String[] args) { int x = 1+2; String s = "Hello" + " world"; System.out.println(x); System.out.println(s); } |

**Output**

3

Hello world

**Subtraction operator**

Minus (-) operator is used for subtracting the second operand from first.

1 2 3 4 5 |
public static void main(String[] args) { int x = 1; int y = 12 - x; System.out.println(y); } |

**Output**

11

**Multiplication Operator**

Multiplication (*) operator used for multiply two numbers.

In following illustration 12*2 gives 24.

1 2 3 4 5 |
public static void main(String[] args) { int x = 1; int y = 12 * 2; System.out.println(y); } |

**Output**

24

**Division Operator**

Division operator (/) used for divide numerator by de-numerator and it gives the quotient.

In the following illustration 12 divided by 2 and the result is 6.

1 2 3 4 5 |
public static void main(String[] args) { int x = 1; int y = 12 / 2; System.out.println(y); } |

**Output**

6

**Modulus operator**

Modulus operator used to returns the remainder of one number divided by another.

In following illustration 13%2 and remainder are 1.

1 2 3 4 5 |
public static void main(String[] args) { int mod = 13 % 2; System.out.println(mod); } |

**Output**

1

**Increment operator**

Increment operator is a unary operator that increments its operand by one. This operator can be used for both postfix(x++) as well as a prefix (++x).

**Postfix-**In the postfix form, it evaluates to the value of the operand before the increment operation.

Case 1

1 2 3 4 |
public static void main(String[] args) { int x = 10; System.out.println(x++); } |

**Output**

10

**Prefix-**In the prefix form it evaluates to the value of the operand after the increment operation.

Case 2

1 2 3 4 |
public static void main(String[] args) { int x = 10; System.out.println(++x); } |

**Output**

11

In case 1, the value got printed first and the value incremented, whereas in case 2, the value of x is incremented first and then the value 11 got printed. So do you now understood the difference?

In prefix, the value of the variable will get incremented first and then the statement will get executed. Whereas in postfix, the statement will be executed with the current value of the variable and then the value gets incremented

**Decrement operator:**

Decrement operator is also a unary operator that decrement its operant by one. This operator can be used for both postfix(x–) as well as a prefix (–x).

**Postfix-**In the postfix form, it evaluates to the value of the operand before the decrement operation.

Case 1

1 2 3 4 |
public static void main(String[] args) { int x = 10; System.out.println(x--); } |

**Output**

10

**Prefix-**In the prefix form it evaluates to the value of the operand after the increment operation.

Case 2

1 2 3 4 |
public static void main(String[] args) { int x = 10; System.out.println(--x); } |

**Output**

9

In case 1, the value got printed first and then value decremented, whereas in case 2, the value of x is decremented first and then the value 9 got printed.

Contributed by: Poonam Tomar

## Leave a Reply