This article explains the arithmetic operators in Java.

### Arithmetic Operators

These operators perform fundamental arithmetic operations like addition, subtraction, multiplication, division etc.

The operands for arithmetic operators must be numeric. For example, these can’t be used on boolean types. These can be used on char types though, since char is subset of int.

These can be binary or unary operators depending on the number of operands.

** **

### Binary operators

These operators act on two operands at a time.

Here is a list :

Operator | Meaning | Example | Result |

+ | Addition operator | 2+3 | 5 |

– | Subtraction operator | 5-3 | 2 |

* | Multiplication operator | 2*3 | 6 |

/ | Division operator | 4/2 | 2 |

% | Modulus or remainder operator | 12%5 | 2 |

Here is an example :

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public static void main(String[] args) { // TODO Auto-generated method stub int x = 10, y =2; int add = x + y; System.out.println(add); int subtract = x - y; System.out.println(subtract); int multiply = x * y; System.out.println(multiply); int divide = x / y; System.out.println(divide); int mod = x % y; System.out.println(mod); } |

Here is the output of above program :

12

8

20

5

0

### Unary operators

These operators act on only one operand.

These include :

- Unary plus (+) and minus (-) operators
- Increment operator (++)
- Decrement operator (–)

The **unary minus** operator negates the operand value. The **unary plus** operator does not change the operand value.

The **increment operator** ++ increase the value by 1.

When ++ is used before a variable, its pre-increment and when ++ is added after a variable its post-increment.

In pre-increment, the value of the variable is incremented first and rest operations are performed later whereas in post-increment, the variable is incremented last.

Here is an example of pre-increment:

1 2 3 4 5 6 | int x =1; System.out.println(x); System.out.println(++x); System.out.println(x); |

This will print :

1

2

2

Now, refer the below example for how post-increment will behave in this scenario:

1 2 3 4 5 6 | int x =1; System.out.println(x); System.out.println(x++); System.out.println(x); |

The output will be :

1

1

2

The **decrement operator** — decreases the operand value by 1.

Similar to increment operator, decrement could be pre-decrement or post-decrement depending on whether – is added before the variable or after the variable.

### Compound Assignment Operators

While using assignment, sometimes we have to use the same variable on both sides of assignment.

For example, x = x+1

For these scenarios, java provides special operators that can be used to combine an arithmetic operation with an assignment.

For example, x = x+1 can be written as :

x += 1

Here, += is the compound assignment operator.

Here is the list of the compound assignment operators:

Operator | Result |

+= | Addition assignment |

-= | Subtraction assignment |

*= | Multiplication assignment |

/= | Division assignment |

%= | Modulus assignment |

© 2015, www.topjavatutorial.com. All rights reserved. On republishing this post, you must provide link to original post