Monday, November 3, 2008

Type Conversion and Casting

Type Conversion and Casting

 

If you have previous programming experience, then you already know that it is fairly common to assign a value of one type to a variable of another type. If the two types are compatible, then Java will perform the conversion automatically. For example, it is always possible to assign an int value to a long variable.

 

However, not all types are compatible, and thus, not all type conversions are implicitly allowed. For instance, there is no conversion defined from double to byte.

 

Fortunately, it is still possible to obtain a conversion between incompatible types. To do so, you must use a cast, which performs an explicit conversion between incompatible types. Let’s look at both automatic type conversions and casting.

 

Java’s Automatic Conversions

 

When one type of data is assigned to another type of variable, an automatic type conversion will take place if the following two 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, so no explicit cast statement is required.

 

For widening conversions, the numeric types, including integer and floating-point types, are compatible with each other. However, the numeric types are not compatible with char or boolean. Also, char and boolean are not compatible with each other.

 

As mentioned earlier, Java also performs an automatic type conversion when storing a literal integer constant into variables of type byte, short, or long.

 

Casting Incompatible Types

 

Although the automatic type conversions are helpful, they will not fulfill all needs. For example, what if you want to assign an int value to a byte variable? This conversion will not be performed automatically, because a byte is smaller than an int. This kind of conversion is sometimes called a narrowing conversion, since you are explicitly making the value narrower so that it will fit into the target type.

 

To create a conversion between two incompatible types, you must use a cast. A cast is simply an explicit type conversion.

 

It has this general form:

 

(target-type) value

 

Here, target-type specifies the desired type to convert the specified value to. For example, the following fragment casts an int to a byte. If the integer’s value is larger than the range of a byte, it will be reduced modulo (the remainder of an integer division by the) byte’s range.

 

int a;

byte b;

// ...

b = (byte) a;

 

A different type of conversion will occur when a floating-point value is assigned to an integer type: truncation. As you know, integers do not have fractional components.

 

Thus, when a floating-point value is assigned to an integer type, the fractional component is lost. For example, if the value 1.23 is assigned to an integer, the resulting value will simply be 1. The 0.23 will have been truncated. Of course, if the size of the whole number component is too large to fit into the target integer type, then that value will be reduced modulo the target type’s range.

 

The following program demonstrates some type conversions that require casts:

 

// Demonstrate casts.

class Conversion {

public static void main(String args[]) {

byte b;

int i = 257;

double d = 323.142;

System.out.println("\nConversion of int to byte.");

b = (byte) i;

System.out.println("i and b " + i + " " + b);

System.out.println("\nConversion of double to int.");

i = (int) d;

System.out.println("d and i " + d + " " + i);

System.out.println("\nConversion of double to byte.");

b = (byte) d;

System.out.println("d and b " + d + " " + b);

}

}

 

This program generates the following output:

LANGUAGE

Conversion of int to byte.

i and b 257 1

Conversion of double to int.

d and i 323.142 323

Conversion of double to byte.

d and b 323.142 67

 

Let’s look at each conversion. When the value 257 is cast into a byte variable, the

result is the remainder of the division of 257 by 256 (the range of a byte), which is 1 in this case. When the d is converted to an int, its fractional component is lost. When d is converted to a byte, its fractional component is lost, and the value is reduced modulo 256, which in this case is 67.

 

Automatic Type Promotion in Expressions

 

In addition to assignments, there is another place where certain type conversions

may occur: in expressions. To see why, consider the following. In an expression, the precision required of an intermediate value will sometimes exceed the range of either operand.

 

For example, examine the following expression:

byte a = 40;

byte b = 50;

byte c = 100;

int d = a * b / c;

 

The result of the intermediate term a * b easily exceeds the range of either of

its byte operands. To handle this kind of problem, Java automatically promotes each byte or short operand to int when evaluating an expression. This means that the sub-expression a * b is performed using integers—not bytes. Thus, 2,000, the result of the intermediate expression, 50 * 40, is legal even though a and b are both specified as type byte.

 

As useful as the automatic promotions are, they can cause confusing compile-time errors. For example, this seemingly correct code causes a problem:

 

byte b = 50;

b = b * 2; // Error! Cannot assign an int to a byte!

 

The code is attempting to store 50*2, a perfectly valid byte value, back into a byte variable. However, because the operands were automatically promoted to int when the expression was evaluated, the result has also been promoted to int.

 

Thus, the result of the expression is now of type int, which cannot be assigned to a byte without the use of a cast. This is true even if, as in this particular case, the value being assigned would still fit in the target type.

 

In cases where you understand the consequences of overflow, you should use an

explicit cast, such as

 

byte b = 50;

b = (byte)(b * 2);

 

which yields the correct value of 100

 

The Type Promotion Rules

 

In addition to the elevation of bytes and shorts to int, Java defines several type promotion rules that apply to expressions. They are as follows. First, all byte and short values are promoted to int, as just described. Then, if one operand is a long, the whole expression is promoted to long. If one operand is a float, the entire expression is promoted to float.

 

If any of the operands is double, the result is double.

 

The following program demonstrates how each value in the expression gets promoted to match the second argument to each binary operator:

 

class Promote {

public static void main(String args[]) {

byte b = 42;

char c = 'a';

short s = 1024;

int i = 50000;

float f = 5.67f;

double d = .1234;

double result = (f * b) + (i / c) - (d * s);

System.out.println((f * b) + " + " + (i / c) + " - " + (d * s));

System.out.println("result = " + result);

}

}

 

Let’s look closely at the type promotions that occur in this line from the program:

LANGUAGE

double result = (f * b) + (i / c) - (d * s);

 

In the first sub-expression, f * b, b is promoted to a float and the result of the sub-expression is float.

 

Next, in the sub-expression i / c, c is promoted to int, and the result is of type int. Then, in d * s, the value of s is promoted to double, and the type of the sub-expression is double. Finally, these three intermediate values, float, int, and double, are considered.

 

The outcome of float plus an int is a float. Then the resultant float minus the last double is promoted to double, which is the type for the final result of the expression.

 

No comments: