Close

Java - Widening and Narrowing Primitive Conversions

[Updated: Mar 9, 2018, Created: Mar 9, 2018]

Widening Primitive Conversion

When a smaller type is converted to a bigger type:

  1. byte to short, int, long, float, or double
  2. short to int, long, float, or double
  3. char to int, long, float, or double
  4. int to long, float, or double
  5. long to float or double
  6. float to double

A widening primitive conversion does not lose information about the overall magnitude of a numeric value.

Narrowing Primitive Conversion

When a bigger type is converted to a smaller type

  1. short to byte or char
  2. char to byte or short
  3. int to byte, short, or char
  4. long to byte, short, char, or int
  5. float to byte, short, char, int, or long
  6. double to byte, short, char, int, long, or float

A narrowing primitive conversion may lose information about the overall magnitude of a numeric value

Combined Widening and Narrowing Primitive Conversion

The following conversion combines both widening and narrowing primitive conversions:

  1. 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

See Also