1. Why is self-documenting code desirable? 1. What happens when we cast a value from one type to another? Why do we have to cast values of variables in Java?
Expert Answer
When we cast a value from one type to another it is assigned the datatype and storage size according to the destination dataype. For Example:
int x = 40;
double y = x; //here the x variab;e gets type casted to the double datatype of storage size 8 bytes and JVM does this implicitly. We have to cast values of variables in Java mostly in case when a function returns a data of type in different form than what we required to perform an operation, JVM does the type casting automatically in most of the cases but sometimes we have to do it explicitly. double d = 55.5; int i = (int) d; //This assigns an integer value of 55 to i by type casting from a decimal value.
|