Type conversion and casting
Type conversion
Section titled “Type conversion”Converting a value from one data type to another.
- Implicit (widening): automatic, safe conversion (smaller type → larger type)
- Explicit (narrowing): manual, may lose information
Implicit conversion (widening)
Section titled “Implicit conversion (widening)”Java automatically converts smaller types to larger types when no data loss is possible.
Widening primitive conversions:
byte → short → int → long → float → double
char → int → long → float → double
Examples:
int intValue = 100;long longValue = intValue; // automaticfloat floatValue = longValue; // automaticdouble doubleValue = floatValue;Assignment context:
byte b = 10;int i = b; // OKArithmetic promotion:
byte a = 5, b = 6;int result = a + b; // result is intIn expressions, byte, short, char are promoted to int before arithmetic.
Explicit casting (narrowing)
Section titled “Explicit casting (narrowing)”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