Java Type Casting in Hindi : Automatic Conversion

आज के लेख में Java Type Casting in Hindi : Automatic Conversion के बारे में जानेगे.

कभी कभी java programming में विभिन्न प्रकार की data value का प्रयोग होता है और उन्हें change करने की आवश्यकता भी पड़ती है. ऐसी स्थितियों में एक data value को दूसरे data value में कन्वर्ट किया जाता है. data value को कन्वर्ट करने के लिए Type Casting method का उपयोग किया जाता है.

Java Type Casting in Hindi : Automatic Conversion

Type Casting क्या है? What is Type Casting

Type casting एक स्थिति है जब एक data type variable को दूसरे data type variable में स्टोर करने की आवश्यकता होती है.

ऐसी स्थितियों में, type नाम को कोष्टक के अन्दर लिखकर एक data type variable को दूसरे data type variable में चेंज कर सकते हैं.

Syntax :

type variable1 = (type) variable2;

एक data type को दूसरे data type में परिवर्तित करने की प्रोसेस को casting कहा जाता है.

उदाहरण के लिए,

int num = 10;

byte n = (byte) num;

long count = (long) num;

Casting की तब जरूरत होती है जब कोई method विभिन्न प्रकार की value return करती है.

उपरोक्त उदाहरण में integer type की value को byte type की वैल्यू में चेंज किया गया है और फिर byte type की value को long type की value में परिवर्तित किया गया है. Equal to (=) एक assignment operator है तथा num, n और count variables है.

Floating point value को integer type value में casting करने पर result में fractional भाग lost हो जाता है.

Conversion Data Type
short, char, int, long, float, double byte
int, long, float, double short
int, long, float, double char
long, float, double int
float, double long
double float

Casting a Value

वैसे जावा में तो ऑटोमेटिक टाइप कन्वर्जन होता है लेकिन जब हम किसी expression या variable का टाइप कन्वर्जन करते हैं तो उसे casting a value कहते हैं.

उदाहरण के लिए

ratio = number1 / number1

number1 और number2 integer data type है लेकिन जब हम इन्हें डिवाइड करेंगे तो रिजल्ट में डेसीमल पार्ट अर्थाात दशमल भाग नहीं आएगा. तो इस प्रकार हमारे expression का सही रिजल्ट नहीं आएगा.

यदि हम number1 को फ्लोटिंग प्वाइंट में टाइप कन्वर्जन करके expression को evaluate करें तो हमें सही रिजल्ट प्राप्त होगा.

ratio = (float) number1 / number2

इस प्रकार के लोकल कन्वर्जन को casting a value कहा जाता है.

Syntax : (type_name) expression

यहां पर type_name एक standard data type है और expression में constant, variable अथवा expression इत्यादि हो सकते हैं. Divide (/) एक arithmetic operator है.

Creation and Casting of Variables

class Typedemo
{
public void main(string args[])
{
char ch = 'a';
byte b = 10;
short = 1993;
int i = 123456789;
long l= 1234567L;
float f = 3.14F;
double = 0.0000123;
System.out.println(" Types Casting ");
short s1 = (short)b;
short s2 = (short)i;
float x = (float)l;
int x1 = (int)f;
System.out.println(" (short)b = " +s1);
System.out.println(" (short)i = " +s2);
System.out.println(" (float)l = " +x);
System.out.println(" (int)f = " +x1);
}
}

Output of the Program

(short)b = 10
(short)i = -13035
(float)l = 1.23445
(int)f = 3 // fractional part is lost

Automatic Conversion

कुछ type के लिए, बिना cast किये हुए एक भिन्न प्रकार के variable में एक प्रकार का मान assign करना संभव है. जावा assigned value का conversion automatically करता है. जिसे automatic type conversion कहा जाता है. Automatic type conversion केवल तभी संभव है जब destination type में सोर्स मान को स्टोर करने की प्रर्याप्त सटीकता हो.

उदाहरण के लिए,

Integer byte की value रखने के लिए पर्याप्त और बड़ा है.

byte b = 70;

int a = b;

यह valid statement है.

एक बड़े data type को छोटे data type में असाइन करने की प्रक्रिया को widening type conversion कहते हैं. और छोटे डाटा टाइप को बड़े डाटा टाइप मे असाइन करने की प्रक्रिया को narrowing type conversion कहते हैं.

Note : narrowing type conversion के result में information loss हो जाती है.


Post a Comment

0 Comments