Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🟢 Foundation  ·  Lesson 08

Type Casting in Java

Type casting क्या है

Type casting का मतलब है एक value को एक data type से दूसरे में बदलना — जैसे int को double में। Java में दो तरह हैं: widening (छोटे से बड़े, अपने-आप) और narrowing (बड़े से छोटे, मैन्युअल)। अंतर जानना data loss और errors बचाता है।

💡 Widening (int → double) अपने-आप और सुरक्षित। Narrowing (double → int) मैन्युअल और data खो सकता है।

Widening (अपने-आप)

int i = 10;
double d = i;      // int -> double, अपने-आप
System.out.println(d);
10.0

छोटे type को बड़े में बदलना (int से double) widening है। यह अपने-आप होता है क्योंकि कोई data नहीं खो सकता — बड़ा type छोटी value आसानी से रखता है।

Narrowing (मैन्युअल)

double d = 9.7;
int i = (int) d;   // double -> int, मैन्युअल cast
System.out.println(i);
9

बड़े type को छोटे में बदलना (double से int) narrowing है। आपको target type brackets में लिखना पड़ता है, (int), क्योंकि data खो सकता है।

Narrowing में data loss

System.out.println((int) 9.7);    // 9  (दशमलव हटा)
System.out.println((int) 9.2);    // 9
System.out.println(Math.round(9.7)); // 10 (rounded)
9
9
10

Double को int cast करना truncate करता है — दशमलव हटा देता है, round नहीं करता। तो (int) 9.7 9 है। ठीक से round करने को, इसके बजाय Math.round() इस्तेमाल कीजिए।

String और number conversion

// String से number: parse, cast नहीं
int n = Integer.parseInt("42");
// number से String:
String s = String.valueOf(42);
System.out.println(n + " " + s);
42 42

आप String और number के बीच cast नहीं कर सकते। text से number जाने को, उसे Integer.parseInt से parse कीजिए। number से text जाने को, String.valueOf इस्तेमाल कीजिए।

Object casting

Object obj = "Hello";
String str = (String) obj;   // String में वापस downcast
System.out.println(str.length());
5

Objects भी cast हो सकते हैं, आमतौर पर जब reference general type (जैसे Object) के रूप में store हो और आप उसे specific type में वापस cast करें। सिर्फ़ उस type में cast कीजिए जो object सच में है, वरना ClassCastException मिलता है।

आम गलतियाँ

  • Narrowing से round की उम्मीद। यह truncate करता है — round को Math.round() इस्तेमाल कीजिए।
  • String को number cast करना। आपको parse करना पड़ता है, cast नहीं।
  • Narrowing को cast भूलना। int i = 9.7; error देता है — (int) 9.7 लिखिए।
  • गलत object cast। जो type object नहीं है उसमें cast करना ClassCastException देता है।

सारांश

  • Type casting value को data types के बीच बदलता है।
  • Widening (छोटा → बड़ा) अपने-आप और सुरक्षित है।
  • Narrowing (बड़ा → छोटा) को मैन्युअल cast चाहिए और data खो सकता है।
  • Double को int cast करना truncate करता है; round को Math.round() इस्तेमाल कीजिए।
  • Strings और numbers को parse करके बदलिए, cast करके नहीं।

आगे, number objects के साथ काम कीजिए wrapper classes में, या Java data types दोहराइए।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

Java में type casting क्या है?
Type casting एक value को एक data type से दूसरे में बदलना है, जैसे int को double में। Java में दो तरह हैं: widening (अपने-आप, बड़े type में) और narrowing (मैन्युअल, छोटे type में)।
Widening और narrowing में क्या अंतर है?
Widening छोटे type को बड़े में अपने-आप और सुरक्षित रूप से बदलता है (int से double)। Narrowing बड़े type को छोटे में बदलता है और cast से मैन्युअल करना पड़ता है, और data खो सकता है (double से int)।
Double को int में कैसे cast करूँ?
Value से पहले brackets में target type डालिए: int n = (int) 9.7;। यह narrowing है, तो दशमलव हिस्सा हट जाता है, 9 देकर — यह truncate करता है, round नहीं।
क्या double को int cast करना number round करता है?
नहीं, यह truncate करता है — बस दशमलव हिस्सा हटा देता है। (int) 9.7 9 देता है, 10 नहीं। Round करने को इसके बजाय Math.round() इस्तेमाल कीजिए।
Casting string parse करने से कैसे अलग है?
Casting int और double जैसे compatible types के बीच बदलता है। Parsing text को number में बदलता है, जैसे Integer.parseInt("5")। आप String को int cast नहीं कर सकते — उसे parse करना पड़ता है।
← Back to Java Tutorial
🔗

Share this topic with a friend

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

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

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

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicUser Input using Scanner आपकी प्रगति: 8 / 61 (13%) · Lesson 9 / 61 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें