LATEST ALERT

Type Casting in Java – Complete Guide with Implicit, Explicit & Object Casting (2026)

Type Casting in Java – Complete Guide

Type Casting in Java – Complete Master Guide

Type Casting in Java

Java programming me kai baar hume ek data type ko dusre data type me convert karna padta hai. Isi process ko Type Casting kaha jata hai. Ye concept especially tab important ho jata hai jab numeric calculations, memory optimization ya object-oriented programming ke sath kaam karte hain.


What is Type Casting?

Type Casting ka matlab hai ek data type ki value ko kisi dusre data type me convert karna. Java me ye do main types me divide hota hai:

  • Implicit Casting (Widening)
  • Explicit Casting (Narrowing)

1️⃣ Implicit Type Casting (Widening Casting)

Jab small data type automatically larger data type me convert ho jata hai to use implicit casting kehte hain. Isme data loss nahi hota.

Conversion Order

byte → short → int → long → float → double

int num = 10; double value = num; System.out.println(value);

Yaha int automatically double me convert ho gaya.

Why It Is Safe?

  • Small se large conversion
  • Memory size increase hoti hai
  • No data loss

2️⃣ Explicit Type Casting (Narrowing Casting)

Jab large data type ko manually smaller data type me convert karte hain to explicit casting hoti hai. Yaha data loss ho sakta hai.

double d = 10.75; int num = (int) d; System.out.println(num);

Output: 10 (Decimal part lost)

Why Data Loss Happens?

  • Decimal remove ho jata hai
  • Range exceed hone par overflow ho sakta hai

Memory Concept Behind Casting

Data Type Size (Bytes)
byte 1
int 4
double 8

Jab large se small me convert karte hain to extra memory cut ho jati hai.


Object Type Casting (OOP Concept)

Java me sirf primitive hi nahi, objects ka bhi casting hota hai. Isko Upcasting aur Downcasting kehte hain.

Upcasting

class Animal {} class Dog extends Animal {} Animal a = new Dog();

Child object ko parent reference me assign karna = Upcasting

Downcasting

Dog d = (Dog) a;

Parent reference ko child type me convert karna = Downcasting


Real Life Example

Implicit casting ko samjho jaise choti bottle ka pani bade bucket me daalna. Explicit casting ko samjho bade bucket ka pani choti bottle me bharna — kuch pani gir sakta hai.


Common Errors

  • ClassCastException
  • Data loss
  • Overflow error

Interview Questions

Q1: What is difference between implicit and explicit casting?

Implicit automatic hota hai, explicit manual.

Q2: Can boolean be casted?

No, boolean cannot be casted to other types.

Q3: What is Upcasting?

Assigning child object to parent reference.


Best Practices

  • Explicit casting carefully use karein
  • Range check karein
  • instanceof operator use karein downcasting me

Conclusion

Type Casting Java ka ek fundamental concept hai. Implicit safe hota hai, explicit me careful rehna padta hai. OOP me object casting ka role bahut important hai.

Related Posts: Variables & Constants | Non-Primitive Data Types

एक टिप्पणी भेजें

0 टिप्पणियाँ

User's Notification>> मध्यम वर्गीय परिवार और निम्न वर्गीय परिवार के साथ एक ही समस्या है एक तो विचारों को हिंदी मीडियम से पढाई करना ,दूसरा टेक्नोलॉजी का ज्ञान शून्य होना, और जरूरी शिक्षा से भी वंचित रहना, आर्थिक स्थिति भी ठीक नहीं होना यह सब पूरी जिंदगी को संघर्ष पूर्ण बना देता है और सफलता के लिए पूरी जिंदगी सफर करना पड़ता है। इन्हीं सब चीजों को ध्यान में रखकर infosaarhub आपके लिए लाया है बेहतरीन अवसर जो नौकरी के साथ-साथ तकनीकी कौशल सीखना चाहते है या कठिन Java ,Python जैसी डिफॉल्ट कोडिंग भाषा को आसान भाषा में सीखना चाहते हैं और सरकारी नौकरी के साथ-साथ पढ़ाई करना चाहते हैं या फिर अपने बिजनेस की तैयारी कर सकते है।