Java Notes
Type casting changes a number from one Java type to another, such as changing an int into a double or a double into an int.
Type casting means changing a value from one data type into another type. You use it when the same information needs a different form—for example, when a whole number should become a decimal for an average.
The important question is: will the new type have enough space and detail for the value? If yes, Java can often change it automatically. If not, Java asks you to write the cast yourself because some information may be lost.
Start with a simple example
An int stores a whole number. A double can store a whole number and a decimal part. Java can safely put an int value into a double variable.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
completedLessons holds the whole number 25. When Java puts it in progress, the value becomes 25.0. Nothing important is lost because a decimal number can represent a whole number too.
Widening: moving to a type that can hold more
This automatic change is called widening. Imagine moving a small amount of water into a larger bottle: the larger bottle can still hold everything from the small bottle.
The common widening direction is:
byte → short → int → long → float → double
You do not need to memorise this entire line today. The useful beginner rule is: Java normally allows a smaller whole-number type to go into a larger or decimal type without extra symbols.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
score starts as an int. Java copies the same value first into longScore, then into decimalScore. You use widening when an API or calculation expects a wider type than the value you already have.
Narrowing: moving to a type that may lose detail
Moving the other way is called narrowing. Imagine trying to pour a large bottle into a smaller one: some liquid may not fit. Java does not do this silently. You must write the target type in parentheses to show that you accept the risk.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
(int) means “take this decimal value and make it an integer.” Java removes the decimal part, so 87.9 becomes 87. It does not round to 88.
Use narrowing only when losing that information is acceptable. For example, a screen that shows completed whole lessons may not need decimal points. A money amount usually should not be narrowed because losing a fraction can be important.
Cast before division when you need a decimal answer
This is the most useful casting situation for beginners. If Java divides one int by another int, it gives a whole-number result before you store it anywhere.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Read the difference carefully:
totalMarks / subjectsdivides two whole numbers first. Java gets82, then stores it as82.0inwrongAverage.(double) totalMarkschanges165into165.0before division.- Now Java performs decimal division, so
correctAveragekeeps82.5.
The cast must happen before the division. Writing (double) (totalMarks / subjects) is too late because the decimal part was already lost.
Converting text into a number is not casting
Sometimes a number comes from text, for example when a user types "42". That value is a String, not an int, even though it looks like a number. Use a parsing method, not a cast.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Integer.parseInt(typedMarks) reads the characters 4 and 2 and creates the number 42. After that, Java can add 8 to it. (int) typedMarks is not valid because a cast cannot turn text into a number.
If the text is not numeric, such as "forty-two", parsing fails. Later, in the Input/Output and Exception Handling lessons, you will learn how to check real user input safely.
Common mistakes
Expecting a cast to round a number
double rating = 4.9;
int wholeRating = (int) rating;wholeRating becomes 4, not 5. A cast removes the fractional part. Use Math.round(rating) when rounding is what you actually need.
Casting after the calculation
// double average = (double) (165 / 2);The division happens first and produces 82, so the final value becomes 82.0. Cast one number before division: (double) 165 / 2.
Try it yourself
Create a class named DailyAverage. Store 17 completed tasks and 6 days in int variables. Print the whole-number result first, then print the exact decimal average.
Hint
Use tasks / days for the whole-number result. For the exact result, write (double) tasks / days.
One working answer
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Run it, then change tasks and days. Check that the first result removes the decimal part while the second keeps it.
Continue the lesson path