← Back to Core Java Course | Chapter 14: Advanced Topics | Lesson 1 of 6

Java की Wrapper Classes

Wrapper classes numbers जैसी simple values को objects में डालती हैं, एक toy को एक gift box में डालने जैसा ताकि यह वहां जा सके जहां सिर्फ boxes allowed हैं। Java उन्हें अपने आप wrap और unwrap करता है।
Syntax
java
Integer object = Integer.valueOf(primitiveValue);
int primitive = object.intValue();
Integer.parseInt(string);

What are Wrapper Classes

Integer, Double, और Boolean जैसी Wrapper classes Java के primitive types के object equivalents प्रदान करती हैं, primitives को कहीं भी उपयोग होने देते हुए जहां एक Object ज़रूरी हो — जैसे generic collections के अंदर।

उदाहरण: What are Wrapper Classes

java
import java.util.ArrayList;
public class Main {
	public static void main(String[] args) {
		ArrayList<Integer> numbers = new ArrayList<>(); // Integer wraps int
		numbers.add(5);
		System.out.println(numbers.get(0));
	}
}

Understanding Autoboxing

Autoboxing Java का किसी primitive को इसके wrapper type में अपने आप convert करना है जब एक object expected हो, जैसे एक int को बिना किसी explicit cast के सीधे एक Integer variable को assign करना।

उदाहरण: Understanding Autoboxing

java
public class Main {
	public static void main(String[] args) {
		Integer boxed = 5; // int automatically becomes Integer
		System.out.println(boxed);
	}
}

Understanding Unboxing

Unboxing इसका उल्टा है: Java अपने आप एक wrapper object से primitive value extract करता है जब एक primitive expected हो, जैसे एक arithmetic expression में एक Integer उपयोग करना।

उदाहरण: Understanding Unboxing

java
public class Main {
	public static void main(String[] args) {
		Integer boxed = 10;
		int primitive = boxed + 5; // Integer automatically becomes int
		System.out.println(primitive);
	}
}

Parsing Numeric Strings

Integer.parseInt() और Double.parseDouble() जैसे methods एक String को इसकी corresponding primitive value में convert करते हैं, अगर string एक valid number न हो तो NumberFormatException throw करते हुए।

उदाहरण: Parsing Numeric Strings

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare num and set it to Integer.parseInt("42")
		int num = Integer.parseInt("42");
		// Declare d and set it to Double.parseDouble("3.14")
		double d = Double.parseDouble("3.14");
		// Print a line to the console
		System.out.println(num + " " + d);
	}
}

Wrapper Constants and Utility Methods

Wrapper classes उपयोगी constants और utilities भी expose करती हैं, जैसे largest representable int के लिए Integer.MAX_VALUE, और </> के लिए एक null-safe comparison alternative के रूप में Integer.compare()।

उदाहरण: Wrapper Constants and Utility Methods

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println(Integer.MAX_VALUE);
		// Print a line to the console
		System.out.println(Integer.compare(3, 7));
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. दो Integer objects को == से compare करना, जो सिर्फ छोटी cached values के लिए काम करता है; equals() उपयोग करें।
  2. एक null wrapper को unboxing करना, जो NullPointerException throw करता है।
  3. एक non-numeric string को Integer.parseInt() से parse करना, जो NumberFormatException throw करता है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.