Numeric promotion in Java

Numeric promotion, recognized as implicit casting or Java's automatic conversions, facilitates the seamless conversion of smaller numeric type values to larger numeric types. This process transpires effortlessly, permitting the smooth execution of arithmetic operations involving integers and floating-point numbers.

In the case of numerical promotion, values of byte, char, and short nature undergo conversion, assuming the form of int values. In turn, int values are further converted to long values whenever deemed necessary. Moreover, the encompassing scope extends to the conversion of long and float values, which transform into double values, adhering to the demand of the situation.

int i = 25; double dbl1 = 2.5; double dbl2 = dbl1 * i;

In the above case, int i is promoted to double so the calculation can be performed. In some ways, you can think of this is analogous to boxing , but boxing involves moving from a struct to an object that is from the stack to the heap. But, using the analogy does give an idea of the fact the integral value is being made into a floating point to perform the calculation.

Widening Primitive Conversion

Widening Primitive Conversion in Java is an automatic process that converts a narrower data type to a wider data type, ensuring no data loss. It allows for seamless assignment and passing of values between compatible data types without explicit casting. The conversion is performed by the Java compiler, promoting the narrower type to a wider type.

This conversion preserves data integrity and is crucial for numeric promotion, enabling arithmetic operations. It applies to numeric primitive types and follows a hierarchy. Widening Primitive Conversion is essential for maintaining type compatibility and data consistency, contributing to the flexibility and compatibility of variable assignments and expressions in Java.

byte to short, int, long, float, or double short to int, long, float, or double char to int, long, float, or double int to long, float, or double long to float or double float to double

Narrowing Primitive Conversion

Narrowing Primitive Conversion in Java involves manually converting a wider data type to a narrower data type. This explicit conversion is performed by the programmer using casting syntax. However, it may result in data loss or truncation due to the smaller range or precision of the target data type. To perform narrowing conversion, the programmer specifies the target data type in parentheses. It should be used when it is known that the value can fit within the narrower data type without significant loss.

The Java compiler provides a warning to alert the programmer about potential data loss or precision issues. Care should be taken to ensure the converted value does not exceed the valid range of the target data type. Narrowing Primitive Conversion offers control but requires caution regarding potential data loss and adherence to valid ranges.

short to byte or char char to byte or short int to byte, short, or char long to byte, short, char, or int float to byte, short, char, int, or long double to byte, short, char, int, long, or float