Data Conversion
What You Will Learn
  • Widening Conversion vs Narrowing Conversion
  • Assignment Conversion
  • Arithmetic Conversion
  • Casting

Data Conversion

Because Java is a strongly typed language, each data value is associated with a particular type. It is sometimes helpful or necessary to convert a data value of one type to another type, but we must be careful that we don't lose important information in the process. For example, suppose a double variable that holds the number 23.45 is converted to an int value. Because an int cannot store the fractional part of a number, some information would be lost in the conversion, and the number represented in the int would not keep its original value.

A conversion between one primitive type and another falls into one of two categories: widening conversions and narrowing conversions. Widening conversions are the safest because they usually do not lose information. Converting from an int to a double is a widening conversion.

Narrowing conversions are more likely to lose information than widening conversions. Therefore, in general, they should be avoided. Converting from a double to an int is a narrowing conversion.

Note the boolean values are not mentioned in either widening or narrowing conversions. A boolean value (true or false) cannot be converted to any other primitive type and vice versa.

In Java, conversions can occur in three ways:

Assignment Conversion

Assignment conversion happens when a value of one type is assigned to a variable of another type and the value is converted to the new type. Only widening conversions can be done this way. For example, if money is a double variable and dollars is an int variable, then the following assignment statement automatically converts the value in dollars to a double:

double money = 0.0;
int dollars = 25;

money = dollars;

So, if dollars contains the value 25, after the assignment, money contains the value 25.0. However, if we try to go the other way around and assign money to dollars, the compiler will send an error message saying that a narrowing conversion could lose information. If you really want to do this assignment, you can do something called casting, which we'll get to in a minute.

Arithmetic Conversion

Arithmetic promotion happens automatically when certain arithmetic operators need to change their operands in order to perform the operation. For example, when a floating point value called sum is divided by an integer value called count, the value of count becomes a floating point value automatically, before the division takes place, producing a floating point result:

double sum = 30;
int count = 3;
double result = sum / count;     
// count is cast to double

Casting

Casting is the most general form of conversion in Java. If a conversion can be done at all in a Java program, it can be done using a cast. A cast is a type name in parentheses, placed in front of the value to be converted. For example, to convert money to an integer value, we could put a cast in front of it:

double money = 84.69;
int dollars = 0;

dollars = 
(int)
money;

The cast returns the value in money, cutting off an fractional part. If money contained the value 84.69, then after the assignment, dollars would contain the value 84. Note, however, that the cast does not change the value in money. After the assignment operation is complete, money still contains the value 84.69.

Casts are helful when a value needs to be treated as another type. For example, if we want to divide the integer value total by the integer value count and get a floating point, we could do it as follows:

result = 
(double)
total / count;

First, the cast operator returns a floating point version of the value in total. This operation does not change the value in total. Then, count is treated as a floating point value by arithmetic promotion. Now the division operator will do floating point division. If the cast had not been included, the operation would have done integer division and cut the fraction off before assigning it to result. Also note that because the cast operator has a higher precedence than the division
operator, the cast operates on the value of total, not on the result of the division.

The table below shows a modified version of the order of operations with the cast operator included.

Precedence Level Operator Operation Association
1
()
grouping
not applicable
2
(int)
(double)
cast
right to left
3
*
/
%
multiplication
division
remainder
left to right
4
+
-
addition
subtraction
left to right
5
=
assignment
right to left