Data Conversion, Conversion Techniques and Casting

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. For example, suppose a short variable holds the number 1000 and we try to convert it to a byte value. While a short variable can store a positive number up to 32,767, a byte variable can only store a positive number whose value is less than 128. Therefore, some bits (significance) would be lost in the conversion.

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. They are called widening conversions because they go from one data type to another type that uses an equal to greater amount of space to store the value. All widening conversions that go from an integer type to another integer type, or from a floating point type to another floating point type, preserve the numeric value exactly.

Widening Conversions

From To
byte short, int, long, float or double
short int, long, float or double
char int, long, float or double
int long, float or double
long float or double
float double

Although widening conversions do not lose any information about the magnitude of a value, the widening conversions that result in a floating point value can lose precision. When converting from an int or a long to a float, or from a long to a double, some of the least significant digits may be lost. In this case, the resulting floating point value will be a rounded version of the integer value.

Narrowing Conversions

Narrowing conversions are more likely to lose information than widening conversions. They often go from one type to a type that uses less space to store a value, and therefore some of the information may be compromised. Narrowing conversions can lose both numeric magnitude and precision. Therefore, in general, they should be avoided. An exception to the space-shrinking situation in narrowing conversions is when we convert a byte (8 bits) or short (16 bits) to a char (16 bits). These are still considered narrowing conversions because the sign bit is incorporated into the new character value. Since a character value is unsigned, a negative integer will be converted into a character that has no particular relationship to the numeric value of the original integer.

A Boolean value cannot be converted to any other primitive type and visa versa.

Conversion Techniques

In Java, conversions can occur in three ways:

Assignment Conversion

Assignment conversion occurs when a value of one type is assigned to a variable of another type during which the value is converted to the new type. Only widening conversions can be accomplished through assignment. For example, if money is a float variable and dollars is an int variable, then assigning dollars to float will automatically convert the value in dollars to a float. On the other hand, if we tried to assign money to dollars, the compiler will issue an error message alerting us to the fact that we are attempting a narrowing conversion that could lose information. If we really want to do this assignment, we have to make the conversion explicit by using a cast.

Promotion Conversion

Conversion via promotion occurs automatically when certain operators need to modify 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 is promoted to a floating point value automatically, before the division takes place, producing a floating point result.

Casting Conversion

Casting is the most general form of conversion in Java. If a conversion can be accomplished at all in a Java program, it can be accomplished using a cast. A cast is a Java operator that is specified by a type name in parentheses. It is 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:

   dollars = (int) money;

The cast returns the value of money, truncating the fractional part.



Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2013