Type Casting & Conversion
Type Casting & Conversion
In the previous lessons you declared variables with specific types: int, double, long, and so on. Real programs constantly move data between those types — storing a measured temperature (a double) in an integer field, reading user input (always a String) and treating it as a number, or keeping a running total in a long even though individual counts are int values. This lesson covers every mechanism Java gives you to do that safely.
Two Directions: Widening and Narrowing
Think of the primitive numeric types arranged by size, smallest to largest:
Widening moves up the chain — a smaller type fits perfectly into a larger container, so no information is lost. Java performs widening automatically (implicitly). Narrowing moves down — forcing a large value into a smaller container may lose data, so Java requires you to write an explicit cast.
Implicit (Widening) Conversion
No cast syntax is needed. Java does the work for you:
Explicit (Narrowing) Cast
When you want to go the other direction you write the target type in parentheses before the value:
The syntax is (targetType) expression. The cast does not round; it truncates — the fractional part is simply discarded.
(int) 36.99 gives 36, not 37. If you need proper rounding, use Math.round() first, which returns a long you can then cast to int.
Integer Overflow
The most dangerous aspect of narrowing is overflow: when a value is too large to fit in the target type, the bits are simply cut off and you get a wrong — but silently wrong — result:
Java does not throw an exception here. It silently wraps around. The rule is simple: always check that the value fits in the target type before casting.
Integer.MAX_VALUE and Integer.MIN_VALUE to know the safe range: 2_147_483_647 and -2_147_483_648. Each primitive wrapper class (Long, Short, Byte) has equivalent constants.
char and Numeric Types
A char stores a Unicode code point — an unsigned 16-bit integer. It participates in widening and narrowing just like numeric types:
Parsing Strings to Numbers
Scanner gives you a raw String from the command line (or a file). To do arithmetic you must parse that string into a numeric type. Each wrapper class provides a parse* static method:
NumberFormatException. If the string is not a valid number — for example "12abc" or an empty string — parseInt crashes at runtime. You will learn to handle this with try-catch in the Exceptions tutorial.
Converting Numbers Back to Strings
Going the other way — turning a number into a String — is always safe and never requires a cast. Three common idioms:
Summary
Widening conversions are automatic and safe. Narrowing conversions require an explicit cast, truncate decimals, and can silently overflow — always verify the value fits first. Use Integer.parseInt, Double.parseDouble, and their siblings to convert String input into numbers, and guard against invalid strings. These patterns appear in nearly every Java program you will ever write.