Type Casting
Sooner or later you'll have an int where you need a double , or a double where you need an int . Type casting is how you convert between them — sometimes safely and automatically, sometimes manually and at your own risk.
Learn Type Casting in our free Java course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Java course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
Going from a smaller type to a larger one is called widening . Because the bigger type can hold every value the smaller one could, no data is ever lost — so Java performs it automatically with no special syntax.
💡 Analogy: Widening is pouring a small cup of water into a big bucket — everything fits, nothing spills. Java is happy to do it for you.
Going from a larger type to a smaller one is narrowing . Data can be lost, so Java refuses to do it silently. You must write an explicit cast: (targetType) value .
💡 Analogy: Narrowing is pouring a full bucket into a small cup. Whatever doesn't fit overflows onto the floor and is gone forever.
Beginners constantly confuse these. Pick the tool that matches your intent:
Math.ceil and Math.floor return a double ; Math.round returns a long . Cast to int if that's what you need.
This is the single biggest source of confusion. Casting converts between numeric types. Parsing turns a String of digits into a number. They are completely different operations.
Going the other way (number → text) is easy: concatenate with "" or call String.valueOf .
Unscramble these lines so the program reads the text "7.5", multiplies by 3, and prints the whole-number part.
Why: You must parse the text into hours before using it. result depends on hours , so it comes next (22.5). Then the cast (int) result truncates 22.5 to 22, which prints last. Each variable must be declared before it is referenced.
9 . The cast truncates toward zero — it does not round to 10.
2.0 . a / 2 is int-by-int division giving 2; only then is the 2 widened to 2.0 . To get 2.5 you'd write a / 2.0 .
20 . Parsing turns "12" into the int 12, so this is real arithmetic, not concatenation.
You can now convert between Java's numeric types with confidence: widening is automatic, narrowing needs an explicit (type) cast, casting truncates while Math.round rounds, and turning text into numbers is parsing, not casting.
Next up: User Input with Scanner — read numbers and text typed by the user and put your casting skills to work.
Practice quiz
What does (int) 3.99 evaluate to?
- 4
- 3.99
- 3
- 0
Answer: 3. Casting a double to int truncates toward zero - it does NOT round - so the .99 is discarded, giving 3.
Why is widening automatic but narrowing requires an explicit cast?
- Widening can never lose data; narrowing can, so Java forces you to signal you accept the loss
- Widening is slower
- Narrowing is impossible in Java
- Both require a cast
Answer: Widening can never lose data; narrowing can, so Java forces you to signal you accept the loss. A bigger type holds every value the smaller one could, so widening is safe and silent. Narrowing can drop data, so an explicit (type) cast is required.
What does (byte) 300 produce?
- 300
- 127
- It throws an exception
- 44
Answer: 44. 300 doesn't fit in a byte (-128..127), so it wraps silently using the low 8 bits, giving 44.
What does this print? int a = 5; double b = a / 2; System.out.println(b);
- 2.5
- 2.0
- 2
- 3.0
Answer: 2.0. a / 2 is int-by-int division giving 2; only then is it widened to 2.0. To get 2.5 use a / 2.0.
What is the difference between casting and parsing?
- Casting converts between numeric types; parsing turns a String of digits into a number
- They are the same operation
- Parsing only works on doubles
- Casting works on Strings too
Answer: Casting converts between numeric types; parsing turns a String of digits into a number. Casting uses (type) between numeric types. Parsing uses Integer.parseInt / Double.parseDouble to turn text into a number. (int)"25" is a compile error.
What does "5" + 3 evaluate to?
- 8
- "8"
- "53"
- 53 as an int
Answer: "53". The + operator means string concatenation when either side is a String, so 3 becomes "3" and is joined, giving "53".
What does Math.round(86.7) return, and what is its type?
- 86 as an int
- 87 as a long
- 86.7 as a double
- 87 as an int
Answer: 87 as a long. Math.round rounds to the nearest whole number (87) and returns a long. Cast to int if you need an int.
What does Integer.parseInt("12") + 8 evaluate to?
- "128"
- "20"
- 128
- 20
Answer: 20. parseInt turns "12" into the int 12, so this is real arithmetic: 12 + 8 = 20, not concatenation.
What does (double) 5 / 2 evaluate to?
- 2
- 2.5
- 2.0
- 3.0
Answer: 2.5. The cast applies to 5 first, making it 5.0, so 5.0 / 2 is double division giving 2.5.
What does (char) 65 produce?
- '6'
- 65
- 'A'
- 'a'
Answer: 'A'. Casting int 65 to char gives the Unicode/ASCII character at code point 65, which is 'A'.