|
Java automatically converts type when you assign a narrower type to a wider type. If you wish to find out exactly when this type conversion takes place, you must know which data type is wider and which is narrower. The answer lies in the knowledge of primitives and some common sense.
A data type is considered wider when it represents wider range of numbers than the original type. For instance, short is a wider data type than byte because its range (2-15 to 215 -1) is wider than the range of byte (-128 to 127). Therefore, the short can represent all byte values. Figure 10.1 illustrates the non-boolean primitives with the narrowest (byte) represented as the innermost rectangle and the widest is represented as the outermost rectangle (double).
![]() |
Figure 10.1 Non-boolean primitive data types from narrowest type byte to widest double
Figure conveniently places the data types as per their ranges. The byte has the smallest range and hence it is placed in innermost rectangle. This containment hierarchy of rectangles represents the containment of primitives. For example, the rectangle int contains three rectangles for char, short and byte. It means that you can store the char, short or byte value in int variable (without any type change). Java will automatically perform the type conversion. Note that the data types, byte and short are incompatible with data type char. This means a byte and short value cannot be stored into char variable or vice-versa. Therefore, the figure shows char and short and two independent rectangles.
|
|
You may be surprised to see the float, which is represented by 32 bits, is wider than long (represented by 64 bits). The float represents wider range of numbers (with 32 bits) than long can (with 64 bits). This is due to the difference between the internal representation of integral and floating-point primitives in Java. The floating-point primitives are represented as mantissa and exponent (For example, 67 can be represented as float f = 6.7e1f;). In the internal representation of float, 24 bits out of 32 are used for the mantissa (23 for the value, 1 for the sign) and 8 for the exponent. This combination can represent wider range of numbers than 32 and 64 bits of int and long. |
When you change a narrower type to a wider type, you do not loose the magnitude of the value. Therefore, Java automatically converts the type. These type conversions are popularly known as the widening conversions. All of the following are considered as widening type conversions:
When the type is changed from byte to either short, int, long, float or double type.
When the type is changed from short to either int, long, float or double type.
When the type is changed from char to either int, long, float or double type.
When the type is changed from int to either long, float or double type.
When the type is changed from long to either float or double type.
When the type is changed from float to double type.
In short, whenever you change type, if you go from inside (narrower type) to outside (wider type) in figure 10.1, it is a widening conversion. In the earlier example, we assigned an int value intNumber to a double type variable doubleNumber. Java allows it because the type double is wider than int and it can accommodate int value without loosing it. Besides widening conversions, there are other rules that need to be checked during type conversions in primitives. The next sections discuss these rules in the specific contexts such as assignment, method call etc.