Typecasting or Type casting (Computer Programming) is making a variable of one type, act like another type for one single operation. Casting is the explicit translation of a primitive type into another primitive type or an object into another object. Automatic translation is known as ‘Type conversion’ and when we do it explicitly it is ‘Typecasting’. Type conversion is very useful but do not fulfill our requirements. For example there’s no automatic conversion from double or int to byte. To create a conversion between two incompatible types, you must use a cast. A cast is simply an exlicit type conversion.
To type cast something in C programming language, simply put the type of variable you want the actual variable to act as inside parentheses in front of the actual variable.
For example: C Programming
#include <iostream>
using namespace std;
int main()
{
cout<< (char)65 <<"\n";
// The (char) is a typecast, telling the computer to interpret the 65 as a
// character, not as a number. It is going to give the character output of
// the equivalent of the number 65 (It should be the letter A for ASCII).
cin.get();
}
In Java, the Java runtime knows enough about the types and how they are related to do this conversation. In Java, we have two variable categories ‘Primitive’ and ‘Reference’. Therefore, we have ‘Casting Primitives’ and ‘Casting Objects’.
Casting Primitives
Primitive data types are byte, short, int, long, float and double.
Left Hand Variable
=
(Casting) Right hand varialbe
If the Right Hand Variable (RHV) has a large storage size than the Left Hand Variable (LHV), then we need a cast statement.
If the RHV is a float type and the LHV is of the integer type you need a cast statement even through both variables are of the same size.
That means, we want to use casting when larger variable value into a smaller data type value or translations between same sized data types.
Casting to Smaller Type:
To type cast something in C programming language, we simply put the type of variable we want the actual variable to act as inside parentheses in front of the actual variable. This is the same theory use in Java.
For example: Java Programming (Casting double into integer. That is 8 bytes into 4 bytes)
public class Casting
{
public static void main(String[] args)
{
int intProduct = 0;
double x = 2.5;
double y = 1.5;
System.out.println(“The product as double is ” + (x*y));
System.out.println(“The product as integer is “ + (int)(x*y));
}
}
The output is: “The product as double is 3.7” and “The product as integer is 3”. The product given in “The product as integer is 3” is a truncated result. When you cast a floating point type to an integer type, or a double to a float, there may be loss of precision as above example. For example, (int)3.2 truncates the .2 and returns 3. Also, when you cast from a larger integer type to a smaller one, if the number is very large, it might be truncated and return a smaller number.
To explicit cast integer into byte, the syntax is: b = (byte) (50*2);
Remember, Booleans cannot be cast.
Casting Objects:
Similarly, you can cast an object to another class if the two classes are related by inheritance. For example java.lang.Object is the super class for all other classes. We can create many inherited classes of it and then cast between them. But, there’re two types of casting in Java – “Up casting” and “Down casting”. You do not need as explicit cast when up casting, but you need an explicit cast when down casting.