Lessons

Java tutorial

Java Type Casting

Casting converts a value from one numeric type to another. Java can widen an int into a double automatically. Going from a double to an int drops the fraction, so you write the target type in parentheses.

Example

Edit the code and run it again. This runner stays in your browser and covers the syntax used in these lessons.

Output

Run the example to see output here.

What this code does

7 becomes 7.0 when it is stored in a double. The cast (int) turns 9.8 into 9 by dropping the fraction rather than rounding it.