Java Wrapper Classes – Boxing, Unboxing, and Need जावा व्रेपर क्लासेस – बॉक्सिंग, अनबॉक्सिंग और आवश्यकता
What is a Wrapper Class? व्रेपर क्लास क्या है?
A wrapper class in Java is a special type of class whose object contains a primitive data type. In simple terms, it wraps primitive values into an object form.
व्रेपर क्लास जावा में एक विशेष प्रकार की क्लास होती है जिसका ऑब्जेक्ट प्रिमिटिव डेटा टाइप को अपने अंदर संग्रहीत करता है। सरल शब्दों में, यह एक प्रिमिटिव मान को ऑब्जेक्ट में लपेटता है।
🔄 a) Boxing and Autoboxing बॉक्सिंग और ऑटोबॉक्सिंग
Java allows converting a primitive type into its corresponding wrapper object — this is called Boxing. When this happens automatically, it's called Autoboxing.
जावा में प्रिमिटिव डेटा टाइप को उनके संबंधित व्रेपर ऑब्जेक्ट में बदलने की प्रक्रिया को बॉक्सिंग कहा जाता है। जब यह रूपांतरण स्वचालित रूप से होता है तो उसे ऑटोबॉक्सिंग कहते हैं।
-
int
→Integer
-
double
→Double
-
long
→Long
🔁 b) Unboxing and Auto-unboxing अनबॉक्सिंग और ऑटोअनबॉक्सिंग
The reverse of boxing — converting a wrapper object back into a primitive type — is called Unboxing. If done automatically, it's called Auto-unboxing.
बॉक्सिंग की विपरीत प्रक्रिया, जिसमें व्रेपर क्लास ऑब्जेक्ट को फिर से प्रिमिटिव टाइप में बदला जाता है, अनबॉक्सिंग कहलाती है। जब यह प्रक्रिया स्वयं होती है, तो इसे ऑटोअनबॉक्सिंग कहते हैं।
📦 Java Wrapper Class Table जावा व्रेपर क्लास तालिका
Primitive Type | Wrapper Class |
---|---|
boolean | Boolean |
char | Character |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
💻 Example 1 – Primitive to Wrapper (Boxing)
public class WrapperExample1 {
public static void main(String args[]) {int a = 5;Integer i = Integer.valueOf(a); // boxingInteger j = a; // autoboxingSystem.out.println(a + " " + i + " " + j);}}
💻 Example 2 – Wrapper to Primitive (Unboxing)
public class WrapperExample2 {
public static void main(String args[]) {Integer a = new Integer(3);int i = a.intValue(); // unboxingint j = a; // auto-unboxingSystem.out.println(a + " " + i + " " + j);}}
Why Wrapper Classes are Needed? व्रेपर क्लास की आवश्यकता क्यों होती है?
-
They convert primitive types into objects. यह प्रिमिटिव डेटा टाइप को ऑब्जेक्ट में बदलते हैं।
-
Classes in
java.util
work only with objects. java.util पैकेज की क्लासेस केवल ऑब्जेक्ट के साथ काम करती हैं। -
Data structures like ArrayList and Vector store only objects. जैसे डेटा स्ट्रक्चर – ArrayList, Vector केवल ऑब्जेक्ट स्टोर करते हैं।
-
In multithreading, synchronization requires objects. मल्टीथ्रेडिंग में सिंक्रोनाइज़ेशन के लिए ऑब्जेक्ट की आवश्यकता होती है।
Comments
Post a Comment