Narrowing conversion
A narrowing conversion converts a value to a type with a smaller representable range or lower precision, potentially losing information.
100 Java Mistakes and How to Avoid Them
A narrowing conversion converts a value to a type with a smaller representable range or less general numeric domain, potentially discarding information.
- How it is used
- Explains how Java casts and compound assignments can discard numeric information and cause incorrect program behavior.
- How deep it goes
- Works through overflow, bit loss, widening order, and analyzer limits, then stops short of a general numeric-conversion taxonomy.
- Examples
- Uses file lengths, character multiplication, negative bytes, and unsigned right shifts as concrete failure cases.
- What you can do after
- Diagnose unsafe casts and compound assignments, then replace them with range checks, exact conversion, or wider types.
Read how 100 Java Mistakes and How to Avoid Them explains narrowing conversion →
Modern C
A narrowing conversion is a conversion to a type whose representable range or precision cannot preserve all values of the source type.
- How it is used
- Warns against value loss in assignments and mixed-type arithmetic.
- How deep it goes
- Explains fitting values, overflow-related results, precision loss, and mixed signedness, but defers broader type-rule details.
- Examples
- Uses integer assignments, floating-point expressions, and signed-versus-unsigned comparisons to expose harmless and dangerous conversions.
- What you can do after
- Evaluate C conversions for representability, information loss, precision loss, and mixed signedness.
Modern C, Third Edition
A narrowing conversion is a conversion to a type that represents a smaller range or less precise set of values than the source type.
- How it is used
- Uses implicit conversions to evaluate whether assignments and mixed-type operations preserve values safely.
- How deep it goes
- Explains value-fit checks, literal-specific behavior, unsigned modular conversion, and precision loss in mixed arithmetic.
- Examples
- Uses signed and unsigned assignments, hexadecimal and decimal minimum values, and mixed integer-floating-point expressions.
- What you can do after
- Evaluate whether conversions preserve values and identify cases that may lose range, precision, or information.
Read how Modern C, Third Edition explains narrowing conversion →
What to know first
- integerAn integer is a whole-number mathematical value, including negative numbers, zero, and positive numbers, with no fractional part.
- unsigned integerAn unsigned integer is an integer type or representation whose values are nonnegative.
- precisionNumerical precision is the fineness with which a numerical representation distinguishes values, often determined by the number of significant digits or bits available.
- floating-point arithmeticFloating-point arithmetic is numerical computation using finite-precision representations of real numbers, which can introduce rounding and range errors.
Related concepts
- implicit conversionkind of · An implicit conversion is an automatic change of a value from one type or representation to another, performed according to language or library rules without explicit conversion syntax at the use site.
- augmented assignmentused by · Augmented assignment is syntax that combines an operation with assignment to update a variable or object using its current value.
- uniform initializationcontrasts with · Uniform initialization is C++ syntax that uses braces to initialize objects and values.
- data conversionimplemented by · Data conversion is the transformation of a value or data from one type, format, or representation to another, explicitly or implicitly.
- numerical overflowproduces · Numerical overflow occurs when an arithmetic result falls outside the range representable by its numeric type or format.
- widening conversioncontrasts with · A widening conversion converts a value to a type with a broader representable range or compatible numeric domain without requiring an explicit cast or conversion operation.
Questions about narrowing conversion
What is narrowing conversion?
A narrowing conversion converts a value to a type with a smaller representable range or lower precision, potentially losing information.
Which Manning books explain narrowing conversion?
100 Java Mistakes and How to Avoid Them, Modern C and Modern C, Third Edition.
What should I know before learning narrowing conversion?
Manning books that teach narrowing conversion usually introduce integer, unsigned integer, precision and floating-point arithmetic first.
What is the difference between narrowing conversion and uniform initialization?
A narrowing conversion converts a value to a type with a smaller representable range or lower precision, potentially losing information. Uniform initialization is C++ syntax that uses braces to initialize objects and values.
What is narrowing conversion a kind of?
Narrowing conversion is a kind of implicit conversion. An implicit conversion is an automatic change of a value from one type or representation to another, performed according to language or library rules without explicit conversion syntax at the use site.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.


