We have already come across casting quite a number of types. In order to help you, we will now recollect all that we have learnt about casting till now and then learn something in addition to what you have already learnt.
Casting refers to the conversion of one data type to another. When we cast a particular variable, the data type of the variable itself is not altered. For example, in the following code, when we cast, num1 to a double; the variable num1 does not change its data type from int to double. Instead, a new copy of num1 is made and its data type is changed to double.
int num1=3, num2=4;
double result = (double) num1 / num2;
Casting is of two types- up casting and down casting. When we cast a variable to a higher data type it is known as up casting. A higher data type simply indicates that the memory allocation for that data type is higher. For example, int is a higher data type than short as the memory allocated for int is 32 bits which is higher than the memory allocated for a short which is 16 bits. On the other hand, when we cast a variable to a lower data type, it is known as down casting. When we up cast a variable, no information is lost but while down casting a variable, information loss might occur. Consider the following example:
short a = 347;
int b= 347347;
short c = (short)a;
int d = (short) b;
This code would give the output:
As you can see, when an int was cast to a short, data loss had taken place as the number 347347 is too large for a short variable.
Following conversions fall under down casting
byte -> char
short -> byte, char
char -> byte, short
int -> byte, short, char
long -> byte, short, char, int
float -> byte, short, char, int, long
double -> byte, short, char, int, long, float
And the following conversions fall under up casting
byte -> short, int, long, float, double
short -> int, long, float, double
char -> int, long, float, double
int -> long, float, double
long -> float, double
float -> double
Implicit and explicit casting
When casting is performed by the programmer using the cast operator (a pair of parentheses containing the data tope), it is known as explicit casting. When such casting is performed by the compiler, it is known as implicit casting. Implicit casting is performed when operators and operands come into the scene. In order to operate on two operands with an operator, both of them should be of the same type. If they are not, implicit up casting to higher of the two types is performed. Consider an int 'a' and a long 'b'. When 'a' and 'b' are added, 'a' is first implicitly casted to a long and then added to 'b'.
long c=a+b; // a is implicitly cast to a long
Look at another example below:
If we perform the addition of c and s (c+s), we get an int as a result. This is because c is implicitly casted to an int. if we try to store the result in a short or a byte variable; we get a compilation error saying 'possible loss of precision'. Precision here refers to the number of digits. Therefore the following statement is invalid.
short ans= c+s;
We will have to cast the result of c+s into a short explicitly in the following way.
short ans= (short) (c+s);
Note that we require a parentheses around c+s. this is because the cast operator has a higher operator precedence than +. To force the addition to occur first, parentheses are used as shown above.
It is also possible to cast objects from one class type to another class type. We shall look at it when we deal with inheritance. For now, this is all you need to know about casting.