Why are widening conversions safer than narrowing conversions?
Also question is, what is a widening conversion?
A widening conversion changes a value to a data type that can allow for any possible value of the original data. Widening conversions preserve the source value but can change its representation. This occurs if you convert from an integral type to Decimal , or from Char to String .
- Widening Casting (automatically) - converting a smaller type to a larger type size. byte -> short -> char -> int -> long -> float -> double.
- Narrowing Casting (manually) - converting a larger type to a smaller size type. double -> float -> long -> int -> char -> short -> byte.
Simply so, what is widening and narrowing in Java?
Widening is taking place when a small primitive type value is automatically accommodated in a bigger/wider primitive data type. Widening is taking place when a reference variable of a subclass is automatically accommodated in the reference variable of its superclass.
Narrowing refers to passing a higher size data type like int to a lower size data type like short. It may lead to data loss. Casting is required for narrowing conversion. Following program output will be 44.