You are on page 1of 9

In programming, it's common to assign one type of variable to another.

For example, you


might want to assign an int value to a float variable:

int i;
float f;

i = 10;
f = i; // assign an int to a float

When compatible types are mixed in an assignment, the value of the right side is
automatically converted to the type of the left side. In this example, the value in i is
converted into a float and then assigned to f. But because Java has strict type checking,
not all types are compatible and not all type conversions are implicitly allowed. For
example, boolean and int aren't compatible.
When one type of data is assigned to another type of variable, an automatic type
conversion will take place if the following conditions are met:

• The two types are compatible.

• The destination type is larger than the source type.

When these two conditions are met, a widening conversion takes place.

For example, the int type is always large enough to hold all valid byte values, and both int
and byte are integer types, so an automatic conversion from byte to int can be applied.
For widening conversions, the numeric types, including integer and floating-point types,
are all compatible with each other. This program is valid because long to double is a
widening conversion that's automatically performed.
Although there's an automatic conversion from long to double, there's no automatic
conversion from double to long because that's not a widening conversion. The example
code below is invalid.

There are no automatic conversions from the numeric types to char or boolean, and char
and boolean aren't compatible with each other. But you can assign an integer literal to
char.
Casting Incompatible Types

Automatic type conversions won't fulfill all your programming needs because they apply
only to widening conversions between compatible types. For all other cases you must
employ a cast. A cast is an instruction to the compiler to convert one type into another. A
cast requests an explicit type conversion and has this general form:

(target-type) expression

Here, target-type specifies the type to convert the expression to.

If you want to convert the type of the expression x / y to int, you can write

double x, y;
// ...
(int) (x / y)

Even though x and y are of type double, the cast converts the outcome of the expression
to int. Parentheses surround x / y to direct the cast properly. Without them the cast to int
would apply only to the x and not to the outcome of the division. The cast is necessary
because there's no automatic conversion from double to int.
When a cast involves a narrowing conversion, information might be lost.

For example, when you cast a long into a short, information will be lost if the long's value
is greater than the range of a short because the long's high-order bits are removed.

When a floating-point value is cast to an integer type, a fractional component will be lost
as a result of truncation. If the value 1.23 is assigned to an integer, the resulting value will
simply be 1. The 0.23 is lost.
In this program, the cast of (x / y) to int results in the truncation of the fractional
component and information is lost. No information is lost when b is assigned the value
100 because a byte can hold that value. When the program tries to assign b the value 257,
information is lost because 257 exceeds a byte's maximum value. Finally, no information is
lost but a cast is needed when the program assigns a byte value to a char.

The output from the program is:

Integer outcome of x / y: 3
Value of b: 100
Value of b: 1
ch: X
Summary

In this lesson, you learned that there are two types of data type conversions: widening
conversions, in which no data is lost, and narrowing conversions, in which data may be lost
because the data types involved are incompatible.

You learned that a widening conversion will take place automatically as long as the two
data types are compatible and the destination type is larger than the source type.

Finally, you learned that you must perform a cast to initiate a narrowing conversion. To
cast one data type into another, enclose the target data type in parentheses. For example,
if X is data type double, (int) X; converts X to the integer data type.

You might also like