What Type Casting Means
Java is statically typed: every value has a type, and the compiler will not let you silently mix incompatible ones. Type casting is how you convert a value from one type to another - sometimes Java does it for you, and sometimes you have to ask for it explicitly.
There are two directions. Going from a smaller type to a larger one (an int into a double) is safe and happens automatically. Going from a larger type to a smaller one (a double into an int) can lose data, so Java forces you to spell it out with a cast.
Widening: The Automatic Conversion
A widening conversion moves a value into a type with more room. Nothing can be lost, so Java does it for you without a cast:
The int 7 slots into a double and becomes 7.0. The widening order for numbers is byte -> short -> int -> long -> float -> double; assigning leftward-to-rightward along that chain never needs a cast. This is why mixing types in arithmetic often "just works" - Java widens the smaller operand first.
Narrowing: The Explicit Cast
Going the other way - a narrowing conversion - can drop data, so the compiler refuses unless you put the target type in parentheses in front of the value:
(int) pi is the cast. Note what it does: it truncates toward zero, simply discarding the decimal part. 3.9 becomes 3, and -3.9 becomes -3. It does not round. If you want rounding, that is a different tool:
Forgetting that a cast truncates is one of the most common beginner bugs - if your "rounded" number is always one too low, this is usually why.
The Integer Division Trap
This gotcha bites everyone once. When both operands are int, the / operator does integer division and throws away the remainder - before any assignment to a double happens:
a / b is 7 / 2, computed entirely in int math, which gives 3. Widening to double afterward only turns 3 into 3.0 - the .5 is already gone. Casting one operand to double first ((double) a / b) forces the whole expression into floating-point, giving the 3.5 you wanted. You only need to cast one side; Java widens the other to match.
When Narrowing Overflows
A cast does not check whether the value fits. If it does not, the bits are simply chopped and you get a surprising result - no error, no warning:
300 does not fit in a byte, so the high bits are discarded and you get 44. This is "silent" data loss - the program runs fine and gives a wrong answer. Only narrow when you are sure the value fits the smaller type.
Converting Between Numbers and Strings
A String is an object, not a number, so you cannot cast it with (int) - that is a compile error. Conversion uses dedicated methods instead:
Going from text to a number uses Integer.parseInt / Double.parseDouble. If the string is not a valid number (say "hello"), these throw a NumberFormatException. Going from a number to text, use String.valueOf(n) or just concatenate with "" - because n + "" forces the int into a String. Watch out for the difference between "5" + 1 (which gives the string "51") and 5 + 1 (which gives 6); the + means concatenation the moment a String is involved.
Casting Objects
Casting also applies to object references, not just primitives. You can cast a reference to a related type along an inheritance chain - widening to a parent type is automatic, narrowing back down needs an explicit cast and is only safe if the object really is that type:
Object o = "hello";
String s = (String) o; // OK: o actually refers to a String
Integer bad = (Integer) o; // compiles, but throws ClassCastException at runtime
The second cast compiles but blows up at runtime with a ClassCastException because the object is a String, not an Integer. You will meet this properly once you reach inheritance and polymorphism; for now, just know the (Type) syntax is the same idea applied to objects.
Next: if-else
Casting lets you reshape values; the next step is making your program choose between paths based on those values. The if-else statement runs one block when a condition is true and another when it is false - the foundation of every decision your code makes. That is the next page.
Frequently Asked Questions
What is type casting in Java?
Type casting is converting a value from one data type to another. Java does small "widening" conversions automatically (like int to double), but a "narrowing" conversion that could lose data (like double to int) requires an explicit cast: int n = (int) 3.9;.
How do you convert a double to an int in Java?
Put the target type in parentheses before the value: int n = (int) 3.9;. This truncates toward zero (drops the decimal part), so you get 3, not 4. To round instead, use Math.round(3.9), which returns 4.
How do you convert a String to an int in Java?
A String is not a number, so you cannot cast it with (int). Use Integer.parseInt("42") to get an int, or Double.parseDouble("3.14") for a double. If the text is not a valid number, these throw a NumberFormatException.