Skip to content

Conversion of types and projection

Classified conversion

Converting types is nothing more than replacing the type of a variable with another. If it is possible to do it in a safe way and without losing information (data). The compiler allows for the so called "type conversion". classified conversion in an automatic way. In practice, when writing code, we assign variables of one type to another type, e.g. all instructions shown below will be true:

Conversions of numerical types

Sometimes it is necessary to convert from one numerical type to another. The figure below shows the permitted conversion types:

conversions of numeric types

are lossless, only those mentioned above may involve partial loss of data, e.g. the integer 123456789 consists of more digits than can fit into the float type, so in the example below we will lose some precision:

int n = 123456789;
float m = n;
System.out.println(m); // the value 1.23456792E8 is printed
When converting between number types we should remember the following rules:

  • If one of the operands is of the double type, the other will also be converted to the double type.
  • Otherwise, if an operand is of the float type, the other will also be converted to the float type.
  • Otherwise, if an operand is of the long type, the other will also be converted to the long type.
  • Otherwise both operands will be converted to the int type.

When converting types, we can generally accept the principle that a smaller type is converted to a larger bit capacity.

Casting

Sometimes we have to convert a numerical type with a higher bit capacity to a smaller one, e.g. the double type to the int type. This is of course possible, but it may involve the loss of some information. Conversions where information can be lost are called casting.

To perform a casting, place the target type name in round brackets before the name of the variable being cast. The general rule is as follows:

(type_converted) variable_smaller_capacity;

Example:

double n = 99.9989;
int m = (int) n;
System.out.println(m); // value 99 will be displayed
double n = 99.9989;
int m = (int) Math.round(n);
System.out.println(m); // value 100 will be displayed

A mistake will be shown in the construction below:

``java double n = 99.9989; int m = n; // Error!

The above example will end with a compilation error, because we tried to automatically convert from floating-point type to integer `int`, which can't happen because of the risk of data loss (decimal places) - the programmer has to openly **shut down** types in this situation.


The following example shows the use of both conversion and projection:

```java
int valA = 3;
int valB = 4;
double avgIncorrect = (valA + valB) / 2; // no projection, result is 3.0
double avgCorrect = ((double)valA + valB) / 2; // result is the expected value 3.5

In the last line of the above example, we project the value in the valA variable into the double type. Then we add the variables double and int to each other. A conversion from int todoublefollows, which also results indouble. In the next step, the resulting sum (which is of typedouble) is divided by2. Another conversion (int to double) takes place and the result is 3.5.