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
- Show narrowing overflow by casting a large
long
toint
. - Use
Math.addExact
to detect overflow compared to normal+
.