June 2018
Beginner
722 pages
18h 47m
English
The cast operator is used for type casting, assigning a value of one type to a variable of another type. Usually, it is used to enable conversions that would otherwise not be allowed by the compiler. We used type casting, for example, when we discussed integer division, the char type as a numeric type, and assigning a class reference to a variable that has a type of one of the implemented interfaces:
int i1 = 11;int i2 = 3;System.out.println((float)i1 / i2); //prints: 3.6666667System.out.println((int)a); //prints: 97IntrfA intA = (IntrfA)classA;
There are two potential issues to watch while casting:
Read now
Unlock full access