2.6.2.2 - Binary Arithmetic Operators

Binary arithmetic operators require both operands for the operation to perform. Ther is four binary arithmetic operators corresponding and behaving exactly as their math counterparts. These are addition operator +, substraction operator -, multiplication operator * and division operator /. In Java, there is one addtional binary arithmetic operator named modulo or remainder operator %.

Owing the Java's strong typing property, Java compiler typecheks the data types of both operands for conformmity. For conformant operands not needing an explicit cast, the operation is performed with automatic type conversion. Failure to this, an exception is thrown.

A strong warning should be madeabout the danger of overflow or underflow .Here, this is more imminent than while unary incrementing or decrementing operations. In binary operations, data change may reach quickly to exhaust available range of primitive data types.

2.6.2.2.1 - Addition Operator +

When the data types of both operands conform, the action of the arithmetic operator is exactly equivalent of his mathematical counterpart. We can experience it by ourselves with an example:

```package preliminary;
public class ArithmeticOperator {
public static void main(String[] args){
int brick = 0;
int dozen = 1;
brick =  dozen + 1;
System.out.println("Number of Bricks = " + brick + " Dozen(s). ");
} // end main
} //end class ArithmeticOperator
```

The result of this program :

```      Number of Bricks = 2 Dozen(s).
```

The result of the expression containing both the assignment and addition operator,

```brick =  dozen + 1;
```

is straightforward and easily predictable. There is two operators in the expression and the assignment operator has the lowest priority. Therefore, the operation begins with addition operator which has the highest priority. There is two operands in the addition operator, the left one is a variable dozen. The variable dozen is declared ( absolutely it should!) before being the operand to the addition operator. The data type of the variable dozen was declared as primitive int type. The data type of the left operand is noted, now this the tour of the right operand of the additition operator. The right operand of the addition operator is a literal whose data type is not explicitly expressed. This defaults to a primitive int. Now both the values and the data types of the both operands is known, Java compiler begins to accomplish the addition operation by checking the compatibility of both operands. Since they are both numeric they are compatible with each other. Java compiler then begins to typecking of both operands and spot that they are both primitive int data type. In that context, there is not any obstacle to perform the addition operator and it is performed. The resulting value of the addition operator is 2 and the resultant data type is the common data type of the operands of the addition operator, i.e. primitive integer data type int. This is the value and the data type of the right operand of the assignment operator.

Now that the type and the value of the right operand of the assignment operator is determined, Java compiler typecheks the both operands for type conformity and finds the left operand is a variable brick. The data type of the variable brick is declared as primitive integer data type int. It concludes that both operands are of the same data type int and therefore there is no obstacle for assignment. The assignment is realized. This is a destructive assignment, the old value of the variable brick is lost and the new value 2 becomes the actual value of brick.

Addition operator may be applied for adding non-numeric types as strings and characters. We know that adding strings together is called string concatenation from section 2.6.1.1 (assignment). We can concatenate two strings together, two characters together or a string with a character. An application example is given below:

```package preliminary;
public class Concatenation {
public static void main(String[] args){
String city= "ROM";
String lastPart =" \U0041";
String wayToGo = city + lastPart;
System.out.println(wayToGo);
} // end main
} //end class Concatenation

```
```    The result of this program :
ROMA
```

From this application, we can understand that we can use unicode entry points instead of keyboard strokes.

One of the useful applications of the concatenation is converting numerical data to string type. This is important because sometimes we may need string representation of the numerical data. There is many ways to accomplish this, but perhaps the easiest method is to add an empty string to a numerical data. This is shown in the example below:

```package preliminary;
public class NumericalToString {
public static void main(String[] args){
double length = 56.88;
String result = length + "";
System.out.println(result);
} // end main
} //end class NumericalToString
```

The result of this program :

```      56.88
```

This result looks like a number but his actual data type is String.

2.6.2.2.2 - Substraction Operator -

The substraction operator acts exactly as the mathematical substraction. An example is presented below:

```package preliminary;
public class Substraction {
public static void main(String[] args){
double data = 22.77;
double correction = 0.03;
double correctedData = data - correction;
System.out.print(" Corrected Data = ");
System.out.println(correctedData);
} // end main
} //end class Substraction
```

The result of this program :

```      Corrected Data = 22.74
```