Java - Type Casting

Type Casting

long L = 100;      // widening from int to long
int i = (int) 1_000_000_0000L; // narrowing; overflow risk

Use explicit casts when narrowing; be mindful of overflow and truncation.

Try it

  1. Show narrowing overflow by casting a large long to int.
  2. Use Math.addExact to detect overflow compared to normal +.