Explicit (narrowing) casting
Explicit casting, also known as narrowing conversion, is required when assigning a value of a larger data type to a variable of a smaller data type. This can result in data loss (overflow, underflow, or precision loss). The cast operator (type) tells the compiler that you are aware of the potential loss.
Syntax
Section titled “Syntax”targetType variableName = (targetType) expression;Examples
Section titled “Examples”Truncation (floating‑point to integer):
double pi = 3.14159;int intPi = (int) pi; // intPi = 3 (fractional part discarded)Overflow (value out of range):
long big = 300;byte small = (byte) big; // small = 44 (300 % 256 = 44)Sign extension: When converting a signed type, the sign bit is preserved, which may produce unexpected values.
byte b = -50;int i = (int) b; // i = -50 (no loss, widening)// Narrowing back:byte b2 = (byte) i; // still -50char to integer and back:
int code = 65;char ch = (char) code; // ch = 'A'Casting in expressions
Section titled “Casting in expressions”When casting a result of an expression, enclose the expression in parentheses:
double a = 9.7, b = 2.3;int result = (int) (a + b); // result = 12 (a+b = 12.0 → truncated)Potential issues
Section titled “Potential issues”- Precision loss – converting
doubletofloatorintloses fractional part. - Overflow – value may wrap around (e.g., 300 → 44 when casting to
byte). - Unexpected values – sign extension can cause large positive values to become negative.
When to use explicit casting
Section titled “When to use explicit casting”- When you intentionally want to truncate a floating‑point number.
- When interfacing with APIs that require a smaller type and you know the value fits.
- When performing bit manipulation or low‑level operations.
Compiler checks
Section titled “Compiler checks”The compiler prevents implicit narrowing assignments to avoid accidental data loss. You must use a cast to override.
int i = 100;byte b = i; // compilation errorbyte b2 = (byte) i; // OK (but may lose data)