Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟢 Foundation  ·  Lesson 08

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 (int → double) is automatic and safe. Narrowing (double → int) is manual and may lose data.

Widening (automatic)

int i = 10;
double d = i;      // int -> double, automatic
System.out.println(d);
10.0

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);
9

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
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);
42 42

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());
5

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?
Type casting is converting a value from one data type to another, like an int to a double. Java has two kinds: widening (automatic, to a bigger type) and narrowing (manual, to a smaller type).
What is the difference between widening and narrowing?
Widening converts a smaller type to a bigger one automatically and safely (int to double). Narrowing converts a bigger type to a smaller one and must be done manually with a cast, and may lose data (double to int).
How do I cast a double to an int?
Put the target type in brackets before the value: 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?
No, it truncates — it simply drops the decimal part. (int) 9.7 gives 9, not 10. To round, use Math.round() instead.
How is casting different from parsing a string?
Casting converts between compatible types like int and double. Parsing turns text into a number, like Integer.parseInt("5"). You cannot cast a String to an int — you must parse it.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicUser Input using Scanner Your progress: 8 / 61 (13%) · Lesson 9 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free