### Automatic conversions in Integer arithmetic

• Automatic conversion in integer arithmetic operations

• It is extremely inconvenient for programmers to have to write conversion operations when values of different integer types are used in the same arithmetic expression

• Java makes writing programs less painful by providing a number of automatic integer conversions

• Java's automatic integer conversion in arithmetic operations:

• Arithmetic operations on integer types:

 All values are converted to int type before an arithmetic operation (+, −, *, /, %) in performed. However, if one of the values in an arithmetic operation is long, then all values are converted to long type before the arithmetic operation in performed.

• Example 1:

 ``` short a = 2; int b = 3, c; c = a + b; // a (short) is first converted to an int value // Then the addition is performed. ```

• Example 2:

 ``` short a = 2, b = 2; int c; c = a + b; // a and b (short) is first converted to an int value // Then the addition is performed. ```

• Example 3:

 ``` int a = 2; long b = 3, c; c = a + b; // a (int) is first converted to an long value // Then the addition is performed. ```

• Quiz 1: can you spot what is wrong with this program

• Java program with an error:

 ``` public class Caveat2 { public static void main(String[] args) { short a, b, c; a = 2; b = 3; c = a + b; // Compilation error ! } } ```

Can you see why the statement c = a + b will cause a compilation error ?

• Because all value are first converted to int, the RHS a + b will produce a result that is of the type int

• The receiving variable c has the type short

• A int typed value cannot be assigned to a short typed variable

We must use a casting operator:

 ``` c = (short) (a + b); ```

• Note:

• This solution will not work:

 ``` c = (short) a + b; ```

because the casting operator (short) has a higher priority than the + operation

• Quiz 2: can you spot what is wrong with this program

• Java program with an error:

 ``` public class Caveat2 { public static void main(String[] args) { short a, b; a = 2; b = -a; // Compilation error ! } } ```

Can you see why the statement b = -a will cause a compilation error ?

• Because is a unary integer arithmetic operator and all value are first converted to int, so: −a will produce a result that is of the type int

• The receiving variable c has the type short

• A int typed value cannot be assigned to a short typed variable

We must use a casting operator:

 ``` b = (short) (-a); ```

• Note:

• This solution will also work:

 ``` b = (short) - a ; ```

because the casting operator (short) and the unary - have the same priority.

These operators are the Right-to-left associative, so:

 ``` (short) - a evaluated as: ( (short) (- a) ) ```

• Automatic type conversion in the assignment operation

• The automatic safe conversion rule of the assignment operator = is also applicable to integer types

• Recall the safe integer conversions are:

 ``` byte ⇒ short ⇒ int ⇒ long ```

• These assignments are allowed: (because they are safe):

 ``` long a; int b; short c; byte d; /* --------------------------------- Automatic conversion happens in all the following assignments --------------------------------- */ a = b; a = c; a = d; b = c; b = d; c = d; ```

• You need casting operators to perform the following (unsafe) assignments:

 ``` long a; int b; short c; byte d; /* ----------------------------------- Unsafe assignment requires casting ------------------------------------ */ b = (int) a; c = (short) a; c = (short) b; d = (byte) a; d = (byte) b; d = (byte) c; ```

• Overflow

• Range of the integer types in Java:

Type name (keyword)   Range of values
byte   −128 . . . 127
short   −32768 . . . 32767
int   −2,147,483,648 . . . 2,147,483,647
long   −9,223,372,036,854,775,808 . . . 9,223,372,036,854,775,807

• When an operation results in a value that is outside the range of a variable, then there is an overflow error

• Example:

 ``` public class Overflow { public static void main(String[] args) { int x, y, z; long a, b, c; x = 1000000; y = 3000; z = x * y; // 3,000,000,000 is outside the range of int System.out.println(z); a = 1000000; b = 3000; c = a * b; // 3,000,000,000 is within the range of long System.out.println(c); } } ```

Output:

 ``` -1294967296 (not 3000000000 !!!) 3000000000 ```

Explanation:

 You need more than 32 bits to represent the value 3,000,000,000 in the binary number system We do not have sufficient number of bits in an int variable to represent the value 3,000,000,000

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac Overflow.java To run:          java Overflow

• What can you do about overflow ?

 You must know how large the values can be If you are not sure, then use the largest possible data type available.