Kinds of Conversion - Yash-777/ITextReports GitHub Wiki
The following conversion combines both widening and narrowing primitive conversions:
byte to char
First, the byte is converted to an int via widening primitive conversion, and then the resulting int is converted to a char by narrowing primitive conversion.
float f = 10.08f;
int = (int) f; // Decimal data loss.
/*
==>> byte « short « int « long « float « double ==>>
char «
*/
A narrowing primitive conversion may lose information about the overall magnitude of a numeric value and may also lose precision and range.
float f = 10.08f;
int = (int) f; // Decimal data loss.
/*
<<== byte « short « int « long « float « double <<==
char «
*/
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.
Here is the simplest example of autoboxing:
Character ch = 'a';
Converting an object of a wrapper type (Integer) to its corresponding primitive (int) value is called unboxing. The Java compiler applies unboxing when an object of a wrapper class is:
- Passed as a parameter to a method that expects a value of the corresponding primitive type.
- Assigned to a variable of the corresponding primitive type.
BOXING : From type boolean to type Boolean
UN_BOXING : From type Boolean to type boolean