Conversion & Casting
Widening vs narrowing, the truncating cast, String parsing and its failures, the + ordering trap.
Converting between types comes in two flavors with opposite risk profiles.
Widening (safe, automatic) โ smaller type into bigger type, no data loss:
int apples = 12;
double avg = apples; // int โ double, always fine: 12.0
long big = apples; // int โ long, fine
Narrowing (lossy, you must ask for it) โ the compiler refuses silent damage; an explicit cast takes responsibility:
double price = 19.99;
int rounded = (int) price; // 19 โ the fraction is TRUNCATED, not rounded
long huge = 9_876_543_210L;
int small = (int) huge; // compiles; may silently wrap around
(int) truncates toward zero โ for real rounding use Math.round.
String โ number is the other daily conversion, and it can fail:
int n = Integer.parseInt("42"); // 42
double d = Double.parseDouble("3.5");// 3.5
String s = String.valueOf(42); // "42"
Integer.parseInt("4x2"); // NumberFormatException at runtime
parseInt throws when the text is not a number โ you will meet the
try/catch that handles this politely in Module 12; for now, only parse text
you trust.
One more trap: 1 + 2 + "A" is "3A" but "A" + 1 + 2 is "A12" โ
+ works left to right, and once a String appears, everything after it is
glued as text.
Next: arithmetic and its surprises.