Coding/Learn/java/type casting
Java Foundational Course
01.Introduction to Java
02.Your First Java Program
03.Syntax & Code Structure
04.Comments in Java
05.Variables & Declaration
06.Primitive Data Types
07.Numbers & Arithmetic Operators
08.Strings in Java
09.Booleans & Relational Operators
10.Type Casting & Conversion
11.Scanner & Interactive Input
12.Logical Operators (&&, ||, !)
13.If Statements
14.If-Else Statements
15.If-Else-If Ladders
16.Switch Statements
17.While Loops
18.Do-While Loops
19.Standard For Loops
20.Break & Continue Statements
21.Arrays in Java
22.Array Indexing & Traversal
23.Enhanced For-Each Loop
24.Essential String Methods
25.Methods in Java
26.Method Parameters & Arguments
27.Method Overloading
28.Classes & Objects in Java
29.Constructors in Java
30.Exception Handling (try-catch-finally)
31.Capstone Mini Projects
Course Certificate0%

Complete all lessons to earn

LearnType Casting & Conversion
10 / 31 0%
Lesson 10 of 31
6 mins read

Type Casting & Conversion

PreviousNext Lesson
In Plain English

Type casting is when you assign a value of one primitive data type to another type. Java supports Widening Casting (automatic) when moving to a larger type, and Narrowing Casting (manual) when moving to a smaller type where data loss could occur.

The Mental Model

"Widening casting is pouring a small glass of water into a giant bucket: nothing spills. Narrowing casting is pouring a giant bucket into a small cup: you must manually confirm and accept that excess water will overflow."

Deep Dive: How It Works

Widening Casting (Automatic): byte -> short -> char -> int -> long -> float -> double. Safe, no precision loss.

Narrowing Casting (Explicit): double -> float -> long -> int -> char -> short -> byte. Requires explicit syntax `(type) value`.

String conversion: `Integer.parseInt("123")`, `Double.parseDouble("45.67")`, `String.valueOf(100)`.

Syntax Blueprint

int myInt = 9;
double myDouble = myInt; // Widening (automatic)
int backToInt = (int) myDouble; // Narrowing (manual)

Place target type in parentheses for manual narrowing cast: (int) 3.99 becomes 3.

PointCore Rules to Remember

Point star
Truncation on Narrowing: (int) 9.99 truncates the decimal part to 9, not rounding.
Point star
String Parsing: Use Integer.parseInt("42") to convert numeric text into real ints.
Point star
Preventing Division Bugs: Cast operands to double: (double) a / b.

Common Beginner Traps & How to Fix Them

Mistake:Assuming (int) rounds up or down: (int) 7.89 yields 7

Why it happens: Narrowing cast simply chops off the decimal digits.

How to fix: Use Math.round(7.89) if you want standard rounding before casting.

Pro Tip: Use String.valueOf(number) when you need to safely convert any primitive into a String.

Live Interactive Example

Hit Run Code to see it live
Widening and Narrowing In Action
Java 13
1
2
3
4
5
6
7
8
9
10
11
12
13
Output Console
Click "Run Code" to view the rendered output.
How it works: (int) 49.99 truncates to 49. Integer.parseInt turns "150" into 150 so it can be added to 50.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Convert string price to int cents

  • PointParse the string "25.75" into a double named "price".
  • PointCalculate total cents: (int) (price * 100).
  • PointPrint "Cents: " + cents.
1
2
3
4
5
6
7
8
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates