Answered! 1. Why is self-documenting code desirable? 1. What happens when we cast a value from one type to another? Why do…

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

 Self-documenting code is desirable as it is focused on making the code itself clear to the reader hence minimizing the need of comments It is basically accomplished by using descriptive names instead of arbitrary names for the various components of the code such as objects, functions,variables etc. For eg: a variable in a program for flight bookings may be Cargo_allowed_in_flight rather than caif.

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.

 

Still stressed from student homework?
Get quality assistance from academic writers!