Skip to content

Type conversion and casting

Converting a value from one data type to another.

  • Implicit (widening): automatic, safe conversion (smaller type → larger type)
  • Explicit (narrowing): manual, may lose information

Java automatically converts smaller types to larger types when no data loss is possible.

Widening primitive conversions:

byteshortintlongfloatdouble

charintlongfloatdouble

Examples:

int intValue = 100;
long longValue = intValue; // automatic
float floatValue = longValue; // automatic
double doubleValue = floatValue;

Assignment context:

byte b = 10;
int i = b; // OK

Arithmetic promotion:

byte a = 5, b = 6;
int result = a + b; // result is int

In expressions, byte, short, char are promoted to int before arithmetic.

When converting a larger type to a smaller type, you must use a cast operator (type). Information may be lost (overflow, precision loss).

double d = 9.78;
int i = (int) d; // i = 9 (fractional part truncated)

Potential issues:

  • Overflow: when the value exceeds the target range
  • Precision loss: for floating‑point to integer
  • Sign extension: for signed types
long big = 300;
byte b = (byte) big; // b = 44 (overflow: 300 % 256 = 44)

Casting between compatible types:

int intVal = 65;
char ch = (char) intVal; // ch = 'A'

Casting with expressions:

float f = 3.14f;
int i = (int) (f * 2); // parentheses needed