Type Casting in Java
In this lesson: Convert data types in Java — implicit widening, explicit narrowing, parse methods — with examples and output. In Hindi & English.
What is type casting
Type casting means converting a value from one data type to another — for example, an int to a double. Java has two kinds: widening (small to big, automatic) and narrowing (big to small, manual). Knowing the difference avoids data loss and errors.
Widening (automatic)
int i = 10; double d = i; // int -> double, automatic System.out.println(d);
Converting a smaller type to a bigger one (int to double) is widening. It happens automatically because no data can be lost — a bigger type easily holds a smaller value.
Narrowing (manual)
double d = 9.7; int i = (int) d; // double -> int, manual cast System.out.println(i);
Converting a bigger type to a smaller one (double to int) is narrowing. You must write the target type in brackets, (int), because data can be lost.
Data loss in narrowing
System.out.println((int) 9.7); // 9 (decimal dropped) System.out.println((int) 9.2); // 9 System.out.println(Math.round(9.7)); // 10 (rounded)
9
10
Casting a double to int truncates — it drops the decimal, it does not round. So (int) 9.7 is 9. To round properly, use Math.round() instead.
String and number conversion
// String to number: parse, not cast
int n = Integer.parseInt("42");
// number to String:
String s = String.valueOf(42);
System.out.println(n + " " + s);
You cannot cast between a String and a number. To go from text to a number, parse it with Integer.parseInt. To go from a number to text, use String.valueOf.
Object casting
Object obj = "Hello"; String str = (String) obj; // downcast back to String System.out.println(str.length());
Objects can also be cast, usually when a reference is stored as a general type (like Object) and you cast it back to the specific type. Cast only to a type the object really is, or you get a ClassCastException.
Common mistakes
- Expecting narrowing to round. It truncates — use
Math.round()to round. - Casting a String to a number. You must parse, not cast.
- Forgetting the cast for narrowing.
int i = 9.7;errors — write(int) 9.7. - Wrong object cast. Casting to a type the object is not throws ClassCastException.
Summary
- Type casting converts a value between data types.
- Widening (small → big) is automatic and safe.
- Narrowing (big → small) needs a manual cast and may lose data.
- Casting a double to int truncates; use
Math.round()to round. - Convert strings and numbers by parsing, not casting.
Next, work with number objects in wrapper classes, or revise Java data types.
Frequently Asked Questions
What is type casting in Java?
What is the difference between widening and narrowing?
How do I cast a double to an int?
int n = (int) 9.7;. This is narrowing, so the decimal part is dropped, giving 9 — it truncates, it does not round.Does casting a double to int round the number?
(int) 9.7 gives 9, not 10. To round, use Math.round() instead.How is casting different from parsing a string?
Integer.parseInt("5"). You cannot cast a String to an int — you must parse it.