## Arithmetic Operators

The Java programming language provides operators that perform addition, subtraction, multiplication, and division. Such operators are called Arithmetic operators. There is a good chance you will recognize them by their counterparts in basic mathematics. The only symbol that might look new to you is “%”, which divides one operand by another and returns the remainder as its result. Here is the list of these operators:

 + addition operator (also used for String concatenation – subtraction operator (also used as unary minus) * multiplication operator / division operator % remainder operator (or modulus operator)

The operands acted upon by these operators must represent numeric values. The operands can be integer quantities or floating-point quantities. The modulus operator in Java works with both integer and floating point numbers. The division and modulus operators require that the second operand be non-zero. Division of an integer by integer results in an integer. But the division of floating by integer or integer by floating or floating by 1 floating number results in floating value. For example:

Let a = 23, b = 12

 Expression Value a + b 35 a – b 11 a * b 276 a / b 1 a % b 11 a + b + ‘C’ 102

Here ‘C’ is encoded as 67 because of its UNICODE, which is added to 35 (sum of a and b).

(Let a = 23.58, b = 5)

 Expression Value a / b 4.716 a % b 3.58

(Let a = 23, b = 2.5)

 Expression Value a / b 9.2 a % b 0.5

The precedence of operators decides the order of execution of operators. The operators with higher precedence are executed first. The arithmetic operators have two levels of precedence i.e. (*, /, %) have higher precedence than (+,  -). If more than one operator of the same precedence appears in the arithmetic expression then the order of execution among these operators is decided by the associativity rule. The associativity among these arithmetic operators is left and right. To understand these rules consider the following arithmetic expression:

2 + 3 + 4 – 12 / 2

Before solving this expression make the following observations:

1. This expression has four operators: +, *, -, and /.
2. The precedence of * and / is more than + and -, so * and / will be evaluated before + and -. (precedence rule)
3. Among * and /, the * operator appears toward the left so * and / will be evaluated before /. Similarly + will be evaluated before -. (associativity rule)

The expression will be evaluated as:

2+3*4-12/2

= 2+12-12/2  // ‘*’ is solved
= 2+12-6  // ‘/’ is solved
=14-6  // ‘+’ is solved
=8  // ‘-‘ is solved

Program to demonstrate the usage of Arithmetic operators

class ArithmeticDemo
{
public static void main(String[] args)
{
int result = 1 + 2; // result is now 3
System.out.println(result);
result = result - 1; // result is now 2
System.out.println(result);
result = result * 2; // result is now 4
System.out.println(result);
result = result / 2; // result is now 2
System.out.println(result);
result = result + 8; // result is now 10
result = result % 7; // result is now 3
System.out.println(result);
}
}

You can also combine the arithmetic operators with the simple assignment operator to create compound assignments. For example, x += 1; x=x+1; both increment the value of x by 1.

The + operator can also be used for concatenating (joining) two strings together, as shown in the following ConcatDemo program:

Program to demonstrate the usage of the + operator to concatenate Strings:

class ConcatDemo
{
public static void main(String[] args)
{
String firstString = "This is";
String secondString = " a concatenated string.";
String thirdString = firstString + secondString;
System.out.println(thirdString);
}
}

By the end of this program, the variable thirdString contains “This is a concatenated string.”, which gets printed to standard output.

Note: The modulus operator (%) returns the remainder of a division operation. It can be applied to floating-point types as well as integer types. This is different from C++ and C languages, where the % operator can only be applied to integer type quantities. The following example program demonstrates the % operator:

Program to demonstrate the % operator:

class ModulusOperator
{
public static void main(String[] args)
{
int x = 42;
double y = 42.25;
System.out.println("x mod 10 = " + x % 10);
System.out.println("y mod 10 = " + y % 10);
}
}

Output:

x mod 10 = 2
y mod 10 = 2.25