 # Arithmetic Operators in Java with Examples

Arithmetic operators are also one of the most used operators in java programming language. Arithmetic operators are used to perform arithmetic operations like addition, subtraction, multiplication and division. These operator performs the same operation as they perform in basic mathematics. Table below shows the list of all arithmetic operators which you can use in your java programs. Consider `a` and `b` as two operands or variables.

Operator Name Example Description
+ Additive operator a+b Adds `a` and `b`
- Subtraction operator a-b Subtracts `b` from `a`
* Multiplication operator a*b Multiplies `a` and `b`
/ Division operator a/b Divides `a` by `b`
% Remainder operator a%b Divides `a` by `b` and returns the remainder

The symbol " `%` " might look new to you but it is also the part of arithmetic operator, it divides one operand by another operand and returns the remainder as it's result. It is also known as modulo operator. The code below shows how to use these operators in java.

``` `int` a = 10, b = 20;
`int` c = a + b; `// value of c = 30`
c = b - a; `// c = 10`
c = a * b; `// c = 200`
c = b / a; `// c = 2`
c = 15 % 2; `// c = 1`
```

### Can we use spaces before and after arithmetic operators ?

Yes, that won't be a problem. For example expressions `5+10`; `5 + 10`; `5 +   10`; are same and will run without any error.

Each operand in an arithmetic operation can be a constant value, a variable/expression, a function call returning a compatible value or a combination of all these.

``` `int` a = 10, b = 20;
`int` c = 5 + 10; `// operands can be a constant value`
c = a + b; `// operands can be a variable/expression`
c = (a - b) + c; `// operands can be a variable/expression`
c = a + sum(5,10); `// operands can be a function call`
```

The addition(+) operator in java is also used for string concatenation. Two strings in java can be concatenated using + operator like below :

``` String s1 = `"refresh"`, s2 = `"java"`;
String s3 = s1 + s2; `// s3 = "refreshjava"`
```

The result of arithmetic operations must be assigned into a variable, not doing so will result in compilation error.

``` `int` a = 10, b = 20;
`a + b;` `// Compilation error, result must be assigned in a variable eg. a = a + b;`
```

The operands in an arithmetic operation can be of different data types as well, except the `boolean` data type. The result of such operation must be assigned in a compatible data type.

``` `int` a = 10, `double` b = 20.5;
`double` d = a + b; `// d = 30.5`
d = a * b; `// d = 205`
`char` c = 'a' + 5; `// c = f`
```

Arithmetic operators can also be combined with assignment operator to create a compound assignment like below :

``` `int` a = 10, b = 20;
a += 10; `// is same as a = a + 10; // += is addition compound assignment operator`
b *= 5; `// is same as b = b * 5; // *= is multiplication compound assignment operator`
```

Refer the assignment operator tutorial to know the complete detail about compound assignment operator in java.

The arithmetic operations must be performed with primitive data type variables only. Performing any arithmetic operations on non primitive data type variables will result as compilation error, except one exception which is the addition operator can be used with `String` data type only for string concatenation.

``` String s1 = `"refresh"`, s2 = `"java"`;
String s3 = s1 - s2; `// Compilation error`
s3 = s1 * s2; `// Compilation error`
s3 = s1 + s2; `// s3 = "refreshjava"`
```

### How many arithmetic operators are there in java ?

There are 5 arithmetic operators in java, + - * / and %.

## Java Modulo or remainder Operator

The `%` operator in java is known as modulo or remainder operator. This operator divides one operand by another operand and returns the remainder as it's result. For example in expression `8%3`, `8` is divided by `3` and the remainder `2` is returned as result.

``` `int` a = 10 % 3  `// a = 1`
a = 10 % 2;  `// a = 0 `
a = 23 % 7;  `// a = 2`
```

The modulo operator can be used with other primitive data types as well except the `boolean` data type.

``` `double` a = 10.5 % 3;  `// a = 1.5`
`double` b = 10.5 % 3.5;  `// b = 0.0`
`float` f = 20.5f % 3.5f;  `// f = 3.0`
```

### Arithmetic operators program in Java

```` class` ArithmeticOperator {
`public static void` main (String[] args) {
`int` num1 = 20, num2 = 10, result;
result = 5 + 10;
System.out.println(`"5 + 10 = "` + result);
result = num1 + num2;
System.out.println(num1 + `" + "`+num2+`" = "` + result);
result = num1 + add(2,3);
System.out.println(`"result with method call = "` + result);

result = num1 - num2;
System.out.println(num1 + `" - "`+num2+`" = "` + result);

result = num1*num2;
System.out.println(num1 + `" * "`+num2+`" = "` + result);

result = num1/num2;
System.out.println(num1 + `" / "`+num2+`" = "` + result);

result = num1 % 7;
System.out.println(num1 + `" % 7 = "` + result);

result += 2; `// Equivalent to result = result+2;`
System.out.println(`"result = "` + result);
}
`static int` add (`int` a, `int` b) {
`return` a + b;
}
} ```

Output:

5 + 10 = 15
20 + 10 = 30
result with method call = 25
20 - 10 = 10
20 * 10 = 200
20 / 10 = 2
20 % 7 = 6
result = 8

Modulo operator is also quite useful in some cases, for example using modulo operator we can check or find, if a number is even or odd, the last digit of a number, if a number is divisible by another number etc. The program below shows how we can check or find these things using modulo operator.

### Java modulo operator program

```` class` ModuloOperator {
`public static void` main (String[] args) {
`int` num1 = 118, num2 = 3;
`// Check if a number is even/odd`
`if`(num1 % 2 == 0)
System.out.printf(`"%d is an even number %n"`,  num1);
`else`
System.out.printf(`"%d is an odd number %n"`, + num1);
`// To find the last digit of a number`
`int` last_digit = num1 % 10;
System.out.printf(`"Last digit of number %d is %d %n"`, num1, last_digit);
`// Check if a num1 is divisible by num2`
`if`(num1 % 3 == 0)
System.out.printf(`"%d is divisible by %d"`, num1, num2);
`else`
System.out.printf(`"%d is not divisible by %d"`, num1, num2);
}
} ```

Output:

118 is an even number
Last digit of number 118 is 8
118 is not divisible by 3

### What is arithmetic expression in java ?

An expression having arithmetic operators is known as arithmetic expression. For example `a+b, a+b*c, a-b*c+d` etc are arithmetic expressions.

### Are all arithmetic operators binary operators ?

Yes all arithmetic operators are binary operators as they require two operands to perform the arithmetic operation.