Page 475 - Introduction to Programming with Java: A Problem Solving Approach
P. 475
11.4 Type Conversions
This section supplements the type casting material you studied in Chapter 3, Section 3.19.
Java is a strongly typed language, so each variable and each value within a program is defined to have a particular data type. As with all strongly typed languages, you need to be careful when working with more than one data type. In this section, you learn how some, but not all, data types convert to other data types. Java makes some type conversions automatically, and it allows you to force some other type conversions.
Either way, be careful. Inappropriate type conversions can cause problems.
To figure out what’s allowed in terms of type conversions, learn the ordering scheme in Figure 11.5.
Crudely speaking, this picture shows what types can “fit inside” other types. For example, a byte value with 8 bits can fit inside a short variable that holds 16 bits because an 8-bit entity is “narrower” than a 16- bit entity. We like the terms “narrower” and “wider” to describe type sizes, but be aware that those are not formal terms; other people do not use those terms. Notice that the boolean type does not appear in this picture. You cannot convert between numeric types and the boolean type.
Figure 11.5 Type conversAionporadergingoschePmeDF Enhancer Promotion
There are two kinds of type conversion—promotion (automatic type conversion) and type casting (forced type conversion). You’ve already seen type casting. We’ll revisit it shortly, but let’s first discuss promotion.
A promotion is an implicit conversion. It’s when an operand’s type is automatically converted without having to use a cast operator. It occurs when there’s an attempt to use a narrower type in a place that expects a wider type; that is, it occurs when you’re going with the flow of the arrows in Figure 11.5. Promotion often oc- curs in assignment statements. If the expression on the right of an assignment statement evaluates to a type that is narrower than the type of the variable on the left of the assignment statement, then during the assignment the narrower type on the right gets promoted to the wider type on the left. Note these promotion examples:
long x = 44;
float y = x;
In the first statement, 44 is an int. The int 44 is narrower than the long x, so the JVM promotes 44 to a long, and then performs the assignment. In the second assignment statement, x is a long. The long x is narrower than the float y, so the JVM promotes x to a float, and then performs the assignment.
Note these additional promotion examples:
11.4 Type Conversions 441
narrower wider
byte short int long float double
char
8 bits 16 bits 32 bits 64 bits 32 bits 64 bits
mixed expressions
double z = 3 + 4.5;
int num = 'f' + 5;
The expressions on the right are mixed expressions. A mixed expression is an expression that contains operands of different data types. Within a mixed expression, the narrower operand automatically promotes