COMP 1010 Midterm: 22MidtermReviewMethods.pdf

106 views33 pages

Document Summary

Casting review widening versus narrowing widening when you convert from one type to a more capable type: more memory, more precision. No information is lost. narrowing when you convert from one type to a less capable type: less memory or less precision. Information is lost in the conversion. note: java does widening conversions automatically and implicitly. For narrowing conversions, java requires an explicit cast examples int i = 1; long l = 1234567890; float f = 3. 14; double d = 1. 2345678901; char c = a"; // int is less capable than float, narrowing examples f = i; L = i; i = c; f = d; i = f; // float is more capable than int, widening. // long is more capable than int, widening. // int is more capable than c, widening. // int is less capable than float, narrowing. Java will do widening conversions automatically. this code is valid. // float is less capable than double, narrowing.