Java Type Casting Basics
In this post, you will learn to:
- State the two types of type casting.
- Describe implicit type casting.
- Describe explicit type casting
Type Casting
In any application, there may be situations where one data type may need to be converted into another data type. The typecasting feature in Java helps in such conversion. Type conversion or typecasting refers to changing an entity of one data type into another. This is done to take advantage of certain features of type hierarchies. For instance, values from a more limited set, such as integers, can be stored in a more compact format. It can be converted later to a different format enabling operations not previously possible, such as division with several decimal places worth of accuracy. In object-oriented programming languages, type conversion allows programs to treat objects of one type as one of their ancestor types to simplify interacting with them.
If conversion results in the loss of precision, as in an int value converted to a short, then the compiler issues an error message unless an explicit cast is made.
The two types of casting are as follows:
- Implicit casting
- Explicit casting
The Figure below shows the two types of casting.
Implicit Type Casting
When one type of data is assigned to a variable of another type, then automatic type conversion takes place, also referred to as implicit type casting, provided it meets the conditions specified:
- The two types should be compatible.
- The destination type should be larger than the source.
The primitive numeric data types that can be implicitly cast are as follows:
- byte (8 bits) to short, int, long, float, double
- short(16 bits) to int, long, float, double
- int (32 bits) to long, float, double
- long(64 bits) to float, double
This is also known as the type promotion rule. The figure below shows the implicit type casting.
The following code demonstrates implicit type conversion.
Code Snippet:
double dbl = 10;
long lng = 100;
int in = 10;
dbl = in; // assigns the integer value to double variable
lng = in; // assigns the integer value to long variable
Note: The type promotion rules are listed as follows:
- All byte and short values are promoted to int type.
- If one operand is long, the whole expression is promoted to long.
- If one operand is float then the whole expression is promoted to float.
- If one operand is double then the whole expression is promoted to double.
Explicit Type Casting
A data type with lower precision, such as short, can be converted to a type of higher precision, such as int, without using explicit casting. However, to convert a higher precision data type to a lower precision data type, such as float to int data type, an explicit cast is required. Otherwise, the compiler will display an error message.
The following code adds a float value to an int and stores the result as an integer.
Code Snippet:
float a = 21.3476f;
int b = (int) a + 5;
The float value in a variable is converted into an integer value 21. It is then added to 5, and the resulting value, 26, is stored in b. This type of conversion is known as truncation. The fractional component is lost when a floating-point is assigned to an integer type, resulting in the loss of precision.
The figure below shows the explicit type casting of data types.
There are several kinds of explicit conversions. They are as follows:
- checked: Before the conversion is performed, a runtime check is done to see if the destination type can hold the source value. If not, an error condition is raised.
- unchecked: No check is performed. If the destination type cannot hold the source value, the result is undefined.
- bit pattern: The data is not interpreted at all, and its raw bit representation is copied verbatim. This can also be achieved via aliasing.