Type Casting (Type Conversion) in Java जावा में टाइप कास्टिंग (टाइप कन्वर्शन)

Type casting or Type Conversion refers to the process of converting one data type into another within a specific statement or expression. In Java, there are two main types of type casting:

टाइप कास्टिंग या टाइप कन्वर्शन के अंतर्गत किसी विशेष स्टेटमेंट या एक्सप्रेशन में एक डेटा टाइप को दूसरे डेटा टाइप में बदला जाता है। जावा में टाइप कास्टिंग के दो मुख्य प्रकार होते हैं:


a) Widening Casting (Implicitly) वाइडनिंग कास्टिंग (स्वचालित रूप से) :-

Converting a smaller data type into a larger data type is called widening casting or implicit casting.

एक छोटे डेटा टाइप को बड़े डेटा टाइप में परिवर्तित करना वाइडनिंग कास्टिंग या स्वचालित कास्टिंग कहलाता है।

Conversion order (रूपांतरण क्रम):


byte → short → char → int → long → float → double

Example / उदाहरण:

int i = 5;
double j = i; // Implicit casting - from int to double
System.out.println(i); // Output: 5
System.out.println(j); // Output: 5.0

b) Narrowing Casting (Manually) नैरोविंग कास्टिंग (मैन्युअली) :-

Converting a larger data type into a smaller data type is called narrowing casting or explicit casting.
एक बड़े डेटा टाइप को छोटे डेटा टाइप में परिवर्तित करना नैरोविंग कास्टिंग या मैन्युअल कास्टिंग कहलाता है।

Conversion order (रूपांतरण क्रम):
double → float → long → int → char → short → byte

Example / उदाहरण:

double i = 5.76;
int j = (int) i; // Explicit casting - from double to int
System.out.println(i); // Output: 5.76
System.out.println(j); // Output: 5

Conclusion / निष्कर्ष:-

In the first example, Java automatically converts int to double (implicit casting). In the second, the programmer manually converts double to int (explicit casting). These conversions help in data handling and type safety.

पहले उदाहरण में, जावा int को स्वतः double में बदल देता है (इम्प्लिसिट कास्टिंग)। दूसरे में, प्रोग्रामर को double को int में स्वयं बदलना पड़ता है (एक्स्प्लिसिट कास्टिंग)। डेटा टाइप्स को सही ढंग से संभालने के लिए इन दोनों कास्टिंग विधियों की समझ आवश्यक है।

Comments

Popular posts from this blog

What is a Web Browser? वेब ब्राउज़र क्या है?

Java's Support System जावा का सहयोगी तंत्र

The Internet and Java इंटरनेट और जावा का सम्बन्ध