Home | | Web Programming | Automatic Type Promotion in Expressions

Chapter: Java The Complete Reference : The Java Language : Data Types, Variables, and Arrays

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.

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, short, or char operand to int when evaluating an expression. This means that the subexpression 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

 

Java defines several type promotion rules that apply to expressions. They are as follows: First, all byte, short, and char 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 are 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:

 

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

 

In the first subexpression, f * b, b is promoted to a float and the result of the subexpression is float. Next, in the subexpression 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 subexpression 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.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Java The Complete Reference : The Java Language : Data Types, Variables, and Arrays : Automatic Type Promotion in Expressions |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.