This may be the simple answer but I want to know the detail.
// Narrowing Conversion float ff = Integer.MAX_VALUE; int ii = (int)ff; System.out.println("float: " + ff + " int: " + ii); // Reference Conversion Integer integer; Float floatt = 100F; integer = (Integer) floatt; In the above example the narrowing conversion works properly but the same with reference conversion is not working. Detail explanation is highly appreciable.
Integerdoes not inherit fromFloat. Autocasting on object does only work from sub- to superclasses, no exception. Explicit casting is not possible becauseIntegerandFloatdo not have direct connection (the common superclass is not enough for typecasting).floatt.intValue()to convert a float to an int.