Arithmetic Operators in Java

These operators are used in mathematical expressions similar to algebra. The exhaustive list of arithmetic operators is shown in the table below.

For example purpose, assume that two integer variables X and Y hold value 16 and 4 respectively. Note that value of both X and Y are fixed throughout the examples in this table.
OperatorOperator NameTypeExample
++Incrementunary++X returns 17
--Decrementunary--X returns 15
+Additionunary / binaryX + Y returns 20
-Subtractionunary / binaryX - Y returns 12
*MultiplicationbinaryX * Y returns 64
/DivisionbinaryX / Y returns 4
%ModulusbinaryX % Y returns 0

Note that:

  • The operands of arithmetic operators must be of Numeric type i.e. byte, short, int, long, float or char. char in java is implemented as numeric type; hence arithmetic operators work fine with char data type.
  • An unary operator takes only one operand while a binary operator takes two operands. + and – have both forms. For example in  x = 10 – 5, – takes two operands 10 and 5 (binary form) while in Y = -5, it takes only one operator 5(unary form). Similarly, + also has both unary and binary forms.

An Example on Basic Arithmetic Operators

Addition, subtraction, multiplication and division are basic arithmetic operators.

The output of the above code is:

Two things to notice here are:

  1. The unary + operator simply returns the value of the operand while unary – operator negates the value of the single operand.
  2. There is a difference between floating point division and integer division.
  • Integer division refers to a division operation where both the operands are integers. The final result of an integer division is also an integer. Hence in the example above int1/int2 returns 2 and not 2.5 .
  • If either of the two operands or both the operands of the division operator are of double data type then it is called floating point division. The result of a floating point division is a double and hence dub1/dub2 returns 2.5. So 5.0/2.0 returns 2.5; 5/2.0 returns 2.5; 5.0/2 returns 2.5 but 5/2 returns 2.

Arithmetic Compound Assignment Operators

All binary arithmetic operators have compound assignment form i.e. *=, /=, %=, +=, -= . So for a variable x if you want to add 5 to it, you can either write x = x+5 or x += 5. Both are equivalent. However, the compound forms are optimized and work slightly faster.

The output of the above code is:

 


The Modulus Operator

Modulus operator (%) returns the remainder of a division operation. It can be applied to integers as well as floating point numbers.

The output of the above code is

 


Increment and Decrement Operators

++ and — are unary operators and have two forms:

  1. prefix and
  2. postfix.

In the prefix form the operator is used before the variable( ++x ) while in postfix form the operator is used after the vaiable( x++ ). In prefix form value is incremented / decremented then obtained for use while in postfix form, the operand is used in the expression then its value is incremented/decremented.

In the first print statement, value of a is incremented and then supplied for printing while in second, the value of b is supplied for printing and then b is incremented. So the output is:

The decrement operator(–) works the same way.

It produces the output: