Читать книгу OCP Oracle Certified Professional Java SE 17 Developer Study Guide - Jeanne Boyarsky - Страница 148
Applying Casting
ОглавлениеWe can fix three of the previous examples by casting the results to a smaller data type. Remember, casting primitives is required any time you are going from a larger numerical data type to a smaller numerical data type, or converting from a floating-point number to an integral value.
int fish = (int)1.0; short bird = (short)1921222; // Stored as 20678 int mammal = (int)9f;
What about applying casting to the last example?
long reptile = (long)192301398193810323; // DOES NOT COMPILE
This still does not compile because the value is first interpreted as an int
by the compiler and is out of range. The following fixes this code without requiring casting:
long reptile = 192301398193810323L;