Casting in Java:
Primitive variables can store values only of the type they are declared to be. However, there are occasions where we want to be able to convert from one type to another. Sometimes types can be converted automatically, but when we have to specify a type conversion it is known as casting.
Casting involves writing the desired type of an expression in parentheses in front of the expression; for example, we would write (int) in front of an expression if we wanted it to be converted to an int.
The full rules of type conversion are lengthy and we do not want you to learn them. The following discussion aims to give you only the general idea of how casting operates. The most important thing to bear in mind is that casting primitive types can result in loss of information and therefore you must think carefully before casting to decide if this is acceptable.
Casting and information loss
Only variables of the floating-point types can store information about fractional parts of numbers. The fractional parts are lost if casting to an integral type.
It is possible to convert one integral type to another or one ?oating-point type to another; however, because variables of different types occupy different amounts of memory, this can cause information loss.
A cast is always required to convert a floating-point type to an integral type. In other cases, a cast is required if the type you are assigning (on the right-hand side of an equals sign) occupies a larger space in memory than the type you are assigning to (on the left-hand side of an equals sign). When a cast is required, the resulting value is not necessarily the same as the value the cast is applied to.
If you have not specified a cast in a case where a cast is required, the compiler will produce a compilation error with the message that there is a 'possible loss of precision'.
What would happen if we were to assign an int variable to a double?
double doubVal; int intVal; intVal = 2;
doubVal = (double) intVal; // this is allowed doubVal = intVal; // this is also allowed!
Although you can write a cast to convert an int to a double, Java does not require you to do so. This is because an int occupies 32 bits, and a double occupies 64 bits. There is no danger that we lose information in converting the int to a double.
Because this type conversion would not result in information loss, Java does not require you to write a cast, and will perform the conversion automatically. This is known as promotion. After each assignment above, doubVal would contain the value 2.0.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your Casting in Java homework and assignments? Live Casting in Java experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Casting in Java homework help, java assignment help and Casting in Java projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours