We use data type conversion to convert one data type to another. The basic rule is that a number can be automatically converted to several further representations. Otherwise, we can perform the data type conversion with the help of built-in functions.
Java and many other code languages are so-called typed languages. Meaning, that we need to keep track of what data type variables have as we use them for different operations. If we have a problem that an operation cannot execute because the data types do not match, we can solve it in three different ways.
Furthermore, below we will see how we use the three different methods as well as some things that may be good paying attention to when it comes to conversion.
Java and many other programming languages are so-called typed languages
Automatic conversion, also called implicit conversion, is very simple. The operation is performed automatically by the compiler, as there is no risk of losing information. Moreover, this means that the restriction is that we can only use automatic conversion to convert upwards according to
Furthermore, this makes sense if we view a data type as a container. Imagine that in an automatic conversion, it is possible to place data from a small container in a larger, but not the other way around.
int a = 2; double x = a;
The operation above gives the variable x value 2.0 and is allowed to perform because double is a larger data type.
Another short example,
int num = 8 + 7.0;
The above operation is not allowed to perform. Because the variable num to be assigned the value of operation 8 + 7.0 is of data type int, and operand 7.0 is of data type double.
When we cannot convert a data type automatically to another data type, this can instead solve through an explicit type conversion. The developer must order it. If we have an expression with several data types and we then risk losing accuracy in operation, then we need to use explicit type conversion. Explicit type conversion is also known as casting (re-typing).
When we have to convert a variable with higher precision to a variable with lower, we have to force it. As we shall see below, we do this by specifying, within parentheses, which data type we want to convert.
double a = 10.8; int num = (int) a; // double (64 bit) --> int (32 bit)
The operation above will give the variable num the value of 10. Important to note that it lost a large part of the accuracy (decimal part) when it went from a double to an int.
The example from the section on automatic type conversion that could we could no complete is solved by
int num = 8 + (int)7.0;
When we collect information from a user at e.g., a Scanner, then the data from the user ends up in a String. To be able to convert a string to a number (int, double, etc.) and then use it, for, e.g., mathematical calculations, we use embedded methods in Java.
Java has a method for each wrapper class. For example, the class Integer has the method parseInt() that converts a String to an int variable. We will see an example below.
String str = "123"; int intVariabel = Integer.parseInt(str); double doubleVariabel = Double.parseDouble(str);
We can also go the other way by using built-in methods, for example, from a number to a string. Each wrapper class has a method called toString() that helps us with that conversion, write: Wrapper-class.toString(variable name);
// int --> String int intVariabel = 24; String strI = Integer.toString(intVariabel); // double --> String double doubleVariabel = 385.98; String strD = Double.toString(doubleVariabel);
It is essential to pay attention to the accuracy of the different data types for various conversions. Remember that when we convert from a decimal number to an integer, we lose the decimal part. We shall see some examples below of simple errors that can occur if we are careless.
double d = 7,99999999; int i = (int)d; // i = 7
There we have a decimal number d, which is very close to the number 8. However, when we type d into an integer (int), we lose the decimal part, and i = 7 because an int cannot have decimal numbers.
int x = 4; int y = 5; double d = x / y; // d = 0
Here, d = 0. Simply because we perform the division on two integers (int), and since they cannot hold any decimals, then d = 0. If, on the other hand, we convert x and y before performing the division
double d = (double)x / y; // d = 0.8
we get the answer d = 0.8